Simple example uses of different AWS services.
Cloud provisioning with infrastructure as code
-
Install AWS Command Line Interface (CLI)
- Configure the CLI:
aws configure
- Add user for the CLI
- Configure the CLI:
-
Create/update/delete the CloudFormation stack:
aws --region eu-north-1 cloudformation create-stack --stack-name ec2-only --template-body file://cloudformation/ec2.yaml aws --region eu-north-1 cloudformation update-stack --stack-name ec2-only --template-body file://cloudformation/ec2.yaml aws --region eu-north-1 cloudformation delete-stack --stack-name ec2-only
CloudFormation extension optimized for serverless applications
-
Install SAM CLI
python -m pip install aws-sam-cli
-
Build the AWS SAM application using
sam build
command.The
sam build
command processes your AWS SAM template file, application code, and any applicable language-specific files and dependencies, and copies build artifacts in the format and location expected by subsequent steps in your workflow.sam build \ --template sam/lambda.yaml \ --manifest sam/lambda-code/requirements.txt
-
Run our serverless application locally for quick development and testing using Docker and
sam local start-api
command.Note: SAM CLI doesn't support schema validation like API Gateway does, so we have to deploy the SAM stack to check whether the validation is working.
sam local start-api
Send a testing request to the API:
curl \ --header "Content-Type: application/json" \ --request POST \ --data '{"searchTerm": "United States"}' \ http://localhost:3000
-
Package the AWS SAM application using
sam package
command.It creates a ZIP file of your code and dependencies, and uploads it to Amazon S3. It then returns a copy of your AWS SAM template, replacing references to local artifacts with the Amazon S3 location where the command uploaded the artifacts.
Before running the following command, you need to create a S3 bucket for it.
sam package \ --s3-bucket my-cloudformation-templates-12345 \ --output-template-file sam/lambda-packaged.yaml
TODO: is this step needed anymore? https://stackoverflow.com/a/63061658/5524090
-
Deploy the AWS SAM application using
sam deploy
command.sam deploy \ --template sam/lambda-packaged.yaml \ --stack-name my-first-sam-stack \ --capabilities CAPABILITY_IAM \ --region eu-north-1