CLI, CloudShell, CDK, SAM
Explore further
AWS CloudShell - https://docs.aws.amazon.com/cloudshell/latest/userguide/welcome.html
AWS CLI
The AWS Command Line Interface (AWS CLI) enables you to interact with AWS services using commands in your command-line shell.
Setup
Prerequisite: install unzip;
Review Docs - AWS CLI
Install unzip
$ sudo apt install unzipInstall - AWS CLI
Source: AWS Installing or updating the latest version of the AWS CLI
$ curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
$ unzip awscliv2.zip
# Install NEW!! See update below.
$ sudo ./aws/install
You can now run: /usr/local/bin/aws --version
$ aws --version
aws-cli/2.7.35 Python/3.9.11 Linux/5.10.16.3-microsoft-standard-WSL2 exe/x86_64.ubuntu.20 prompt/off
$ which aws
/usr/local/bin/aws
$ ls -l /usr/local/bin/aws
lrwxrwxrwx 1 root root 37 Sep 27 17:45 /usr/local/bin/aws -> /usr/local/aws-cli/v2/current/bin/aws
$//tbd
Configure - AWS CLI
Source: AWS CLI - Quick Setup - New configuration quick setup
For new & clean environment, configure the "default" config and profile
$ aws configure
AWS Access Key ID [None]: <replace-with-access-key-id>
AWS Secret Access Key [None]: <replace-with-access-key>
Default region name [None]: us-west-2
Default output format [None]: jsonThe configurations are stored in the ~/.aws folder for (macOS/Linux); or %USERPROFILE%.aws\ folder for (Windows), in two files: config and credentials
[default]
region=us-west-2[default]
aws_access_key_id = <replace-with-access-key-id>
aws_secret_access_key = <replace-with-access-key>[Optional] Configure for a specific "profile", for example: "produser"
$ aws configure --profile produserDocs - AWS CLI
How-to
CloudShell
Source: AWS CloudShell
Command line access to AWS resources and tools directly from a browser.
There is no additional charge for AWS CloudShell.
Useful commands to explore:
zsh; vim ~/.zshrc
CDK
The AWS Cloud Development Kit (AWS CDK) is a software development framework for defining your cloud application resources using familiar programming languages including: Python, Typescript, etc. Regardless of the programming languages, CDK uses the same back end which runs on Node.js. Therefore, the development computer needs to have the Node.js prerequisite installed.
Source: Getting started with the AWS CDK
Step-by-Step Instructions
Prerequisite - Node.js & NPM; See SETUP > Dev Environment > Node.js
Prerequisite - AWS CLI; See Configure - AWS CLI; and review CDK credential requirements below
[Optional] Setup User Policy
Review Docs - CDK
CDK credentials requirements
Ensure the development computer has been configured with the "region" and "credentials" in the config and credentials files, respectively. Both files are stored in the ~/.aws folder. For more details, see Configure - AWS CLI.
NOTE: although the AWS CDK uses credentials from the same configuration files as the AWS CLI, it behaves differently. If you use a named profile from the credentials file, the config file must have a profile of the same name specifying the region. In addition, do not use a profile named containing the word "default" (e.g. [profile default]). [see Getting started with the AWS CDK]
Install - CDK
$ npm install -g aws-cdk
$ cdk --version
2.29.0 (build 47d7ec4)
$ cdk --help
// To update aws-cdk
$ npm install -g aws-cdkCDK Bootstrapping
Deploying stacks with the AWS CDK requires the AWS CloudFormation stack (called CDKToolkit) and a dedicated Amazon S3 buckets to be available during deployment. The process is called bootstrapping, and it's a onetime setup for the AWS Account. The S3 bucket and the CloudFormation "CDKToolkit" resources are reusable by any cdk projects.
$ cdk bootstrap aws://ACCOUNT-NUMBER/REGION
// Get acct-number from AWS console or
$ aws sts get-caller-identity
// Get the default region for the profile
$ aws configure get region
// cdk bootstrap output
⏳ Bootstrapping environment aws://349<truncated>/us-west-2...
Trusted accounts for deployment: (none)
Trusted accounts for lookup: (none)
Using default execution policy of 'arn:aws:iam::aws:policy/AdministratorAccess'. Pass '--cloudformation-execution-policies' to customize.
CDKToolkit: creating CloudFormation changeset...
✅ Environment aws://349<truncated>/us-west-2 bootstrapped.
// Go to AWS console > S3 to see the newly created bucket:
// cdk-hnb659fds-assets-349<truncate>-us-west-2
During "CDK bootstrapping", the following five IAM Roles are also created:
cdk-<uniqueId>-cfn-exec-role-<account-no>-<region>
cdk-<uniqueId>-deploy-role-<account-no>-<region>
cdk-<uniqueId>-file-publishing-role-<account-no>-<region>
cdk-<uniqueId>-image-publishing-role-<account-no>-<region>
cdk-<uniqueId>-lookup-role-<account-no>-<region>CLEANUP
As mentioned above, the resources created by bootstrapping are reusable across cdk projects so they should not be deleted. However, to completely cleanup all the resources created during CDK bootstrapping:
Delete the S3 bucket
Delete the CloudFormation "CDKToolkit"
Delete the five IAM roles; go to IAM console, and search for "cdk-"
[Optional] Setup User Policy
Define user policy needed to deploy AWS S3.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"cloudformation:DescribeStacks",
"cloudformation:CreateChangeSet",
"cloudformation:DescribeChangeSet",
"cloudformation:ExecuteChangeSet",
"cloudformation:DescribeStackEvents",
"cloudformation:DeleteChangeSet",
"cloudformation:DeleteStack",
"cloudformation:GetTemplate",
"s3:CreateBucket"
],
"Resource": "*"
}
]
}Tutorials
Python - Python Tutorial
Python - Youtube - Getting Started with AWS CDK and Python | Step by Step Tutorial
Docs - CDK
API - Python - PyPi.org - aws-cdk.aws-s3
API - Typescript - tbd
AWS Cloud Development Kit (CDK) v2 - Developer Guides
Troubleshooting
AWS CLI SAM
Source: AWS Serverless Application Model - Installing the AWS SAM CLI
Prerequisite: AWS CLI version 2
Download the AWS SAM CLI .zip file to a directory
$ curl "https://github.com/aws/aws-sam-cli/releases/latest/download/aws-sam-cli-linux-x86_64.zip" \
-o "awssamcli.zip"Verify the integrity and authenticity of the downloaded installer files by generating a hash value using the following command:
$ sha256sum aws-sam-cli-linux-x86_64.zip
24aa5893e35ce370b92b730fe163d902d248bdef4e3513eb3810bc6ff91a4efd aws-sam-cli-linux-x86_64.zipUnzip the installation files into the
sam-installation/subdirectory
$ unzip aws-sam-cli-linux-x86_64.zip -d sam-installationInstall the AWS SAM CLI
$ sudo ./sam-installation/install
$ sam --version
SAM CLI, version 1.73.0References
[REF-1] AWS Configuration and credential file settings
[REF-2] AWS Environment variables to configure the AWS CLI
Last updated