All Projects → aws-samples → aws-serverless-app-sam-cdk

aws-samples / aws-serverless-app-sam-cdk

Licence: MIT-0 license
Serverless app with CI/CD pipeline using AWS SAM and AWS CDK

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to aws-serverless-app-sam-cdk

invoiceless
Serverless backend for sending simple recurring invoices
Stars: ✭ 44 (-4.35%)
Mutual labels:  aws-sam
cdk-aurora-globaldatabase
Use AWS CDK Create Aurora Global Database
Stars: ✭ 17 (-63.04%)
Mutual labels:  aws-cdk
xilution-react-todomvc
An implementation of TodoMVC featuring AWS Serverless Application Model (SAM) and Xilution SaaS.
Stars: ✭ 24 (-47.83%)
Mutual labels:  aws-sam
cdk-py-k8s-the-real-hard-way-aws
Python CDK code for "Kubernetes The (real) Hard Way (AWS)"
Stars: ✭ 14 (-69.57%)
Mutual labels:  aws-cdk
patterns-serverless-scala-minimal
Production-ready setup for starting with serverless Scala on AWS Lambda using AWS CDK for deployment
Stars: ✭ 23 (-50%)
Mutual labels:  aws-cdk
aws-transcribe-demo
A simple AWS demo utilises Amazon Transcribe to convert audio to text and analyse.
Stars: ✭ 39 (-15.22%)
Mutual labels:  aws-cdk
rds-snapshot-export-to-s3-pipeline
RDS Snapshot Export to S3 Pipeline
Stars: ✭ 88 (+91.3%)
Mutual labels:  aws-cdk
cdkgoat
CdkGoat is Bridgecrew's "Vulnerable by Design" AWS CDK repository. CdkGoat is a learning and training project that demonstrates how common configuration errors can find their way into production cloud environments.
Stars: ✭ 27 (-41.3%)
Mutual labels:  aws-cdk
cdk-examples
AWS CDK Examples Repository
Stars: ✭ 49 (+6.52%)
Mutual labels:  aws-cdk
amazon-kinesis-timestream-grafana
Sample serverless application that reads events from Amazon Kinesis Data Streams and batches records to Amazon Timestream via Apache Flink app, visualizing results via Grafana.
Stars: ✭ 16 (-65.22%)
Mutual labels:  aws-cdk
elasticache-geospatial-public-bikes
Sample application that demonstrates use of Redis Geospatial commands using Amazon ElastiCache, AWS Lambda, and Serverless Application Model.
Stars: ✭ 34 (-26.09%)
Mutual labels:  aws-sam
nexus-oss-on-aws
Deploy Sonatype Nexus Repository OSS on AWS with well architecture.
Stars: ✭ 51 (+10.87%)
Mutual labels:  aws-cdk
aws-cdk-github-oidc
CDK constructs to use OpenID Connect for authenticating your Github Action workflow with AWS IAM
Stars: ✭ 59 (+28.26%)
Mutual labels:  aws-cdk
ses-email-forwarding
AWS CDK constructs to receive emails with SES and forward them to any other email address.
Stars: ✭ 17 (-63.04%)
Mutual labels:  aws-cdk
aws-cdk-starter
No description or website provided.
Stars: ✭ 31 (-32.61%)
Mutual labels:  aws-cdk
amazon-cognito-example-for-external-idp
An example for using Amazon Cognito together with an external IdP
Stars: ✭ 222 (+382.61%)
Mutual labels:  aws-cdk
patterns-serverless-rust
Production-ready setup for starting with serverless Rust + GraphQL + DynamoDB on AWS Lambda using AWS CDK for deployment
Stars: ✭ 15 (-67.39%)
Mutual labels:  aws-cdk
aws-sam-typescript-layers-example
Example project for developing AWS Lambda functions on TypeScript with all goodies: local development, tests, debugging, shared layers (3rd party and your own), and deploy.
Stars: ✭ 168 (+265.22%)
Mutual labels:  aws-sam
cfsec
Static analysis for CloudFormation templates to identify common misconfiguration
Stars: ✭ 53 (+15.22%)
Mutual labels:  aws-sam
aws-serverless-using-aws-cdk
This repository provides the basic patterns of AWS Serverless using AWS CDK.
Stars: ✭ 40 (-13.04%)
Mutual labels:  aws-cdk

Serverless using AWS SAM

This repository contains a simple application that gets defined and provisioned using AWS SAM.

For CI/CD it assumes there are two environments: staging and production.

Architecture

Application is responsible for storing a message in a DynamoDB table through an Amazon Lambda function when it is dropped into an SQS queue.

Architecture

Requirements

  • Node.js 12 or above
  • npm 6 or above
  • AWS SAM CLI 0.40.0. It requires you to set AWS credentials so that it can make calls to AWS services on your behalf.
  • Set the AWS region you want to deploy this application to. You can do so through aws configure or AWS_DEFAULT_REGION environment variable.

Project Structure

AWS SAM template is defined in the root directory through a YAML file. It defines:

  • An input Parameter that specifies to which environment we are going to be deploying to
  • A Condition that based on the parameter above determines if this is a deployment to production or not. This is needed as different resources and configurations will be used based on the environment.
  • Global section to define those parameters that are common to multiple resources in the template.
  • SQS queue where new book events are sent to.
  • Lambda function called PutBookFunction which is responsible for taking messages off the queue and storing them into DynamoDB. This component is the core of the application and hence, it is formed by the following configuration:
    • IAM Policies so it can receive messages from SQS and write them into DynamoDB table.
    • For staging, new versions are deployed to an alias named after the environment with a bluen/green approach.
    • For production, we use a more conservative approach that allows us to gradually shift traffic towards the new version. During the time this deployment lasts, a CloudWatch alarm (AliasErrorMetricGreaterThanZeroAlarm) is monitored so in case it throws errors, a rollback to the previous version is performed.
    • Lastly, deployments to both environments perform a check (or smoke test) for the new version before shifting traffic to it through a lambda function (PreTrafficCheckFunction). If it fails, traffic is not routed to the new version and deployment is considered failed.
  • Aforementioned DynamoDB table: Books.

Using SAM to deploy the app

Packaging and deploying the app to AWS is relatively straight forward since all configuration is defined in template.yml.

  • Package your lambda functions and store them safely in a S3 bucket. This command outputs the corresponding version of your template.yml pointing to where your artifacts have been stored in S3.

    sam package --s3-bucket my-artifacts-bucket --s3-prefix my-prefix --output-template-file out.yml
  • Deploy a new version of your app using the artifacts the command above just generated (using staging as the target environment for demo purposes):

    sam deploy --template-file out.yml --stack-name my-stack-staging --parameter-overrides ParameterKey=Environment,ParameterValue=staging --capabilities CAPABILITY_IAM

    You can monitor how the deployment is happening through AWS CodeDeploy as the above will create a new application in this service alongside a deployment group for your lambda.

These two commands will be usesd in both Build and Deploy steps of our pipeline.

Testing your lambda locally

Create a docker network and run a local dynamodb container in it:

docker network create my-network
docker run -d --network my-network -v "$PWD":/dynamodb_local_db -p 8000:8000 \
    --network-alias=dynamodb --name dynamodb \
    amazon/dynamodb-local -jar DynamoDBLocal.jar -sharedDb

Create the following table in the local DynamoDB:

aws dynamodb create-table --table-name books \
    --attribute-definitions AttributeName=isbn,AttributeType=S \
    --key-schema AttributeName=isbn,KeyType=HASH \
    --endpoint-url http://localhost:8000 \
    --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5

Check previous step ran successfully:

aws dynamodb list-tables --endpoint-url http://localhost:8000
aws dynamodb describe-table --table-name books --endpoint-url http://localhost:8000

Finally, test your function with a dummy event (that can be generated with sam local generate-event sqs receive-message):

TABLE=books sam local invoke "PutBookFunction" -e events/sqs_event1.json  --docker-network my-network

Notice that our lambda function will point to the local DynamoDB container for the command above through its HTTP layer. Condition is based on AWS_SAM_LOCAL which automatically gets set by sam when executing local commands.

A very similar approach using Docker will be taken to perform end-to-end tests for our lambda function and its integration with DynamoDB.

Monitoring

AWS SAM CLI allows to monitor any lambda function given its logical id within a stack or its name as we would visualize them using Cloudwatch. For ie:

sam logs -n PutBookFunction --stack-name aws-serverless-app-staging --tail

CI/CD

Source -> Build -> Test -> Deploy to Staging -> Deploy to Production (Manuall approval + Deploy)

Stages and actions are implemented using AWS CodePipeline, AWS CodeBuild and AWS CodeDeploy (behind the scenes through sam deploy command).

Pipeline itself is defined and provisioned by AWS CDK using Typescript.

To get it provisioned follow these steps:

  • First, install AWS CDK tools:

    npm install -g aws-cdk
  • Then, fetch dependencies and provision pipeline:

    cd pipeline
    npm i
    npm run build
    cdk deploy

    There will be a summary of security-related changes that needs to be approved to continue with the CloudFormation template creation.

NOTES:

  • This step needs to be implemented only once when starting to work on this project.
  • Artifacts generated by the pipeline are stored in an Amazon S3 bucket called ci-cd-pipeline-artifacts.
  • Artifacts which are specific for our application are stored in an Amazon S3 bucket called aws-serverless-app-artifacts.
  • Aforementioned buckets are created by our CDK script and hence, they cannot exist before running it.
  • To successfully connect to Github:
    • Username is provided through a AWS SSM parameter called github_username,
    • Secret is provided through AWS Secrets Manager. Both secret name and field are called github_token.

Want to contribute?

Check our contribution guidelines before submitting a pull request. Any contribution must be done to the develop branch.

License

This library is licensed under the MIT-0 License. See the LICENSE file.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].