Skip to content

alazaroc/blog-backend-infrastructure

Repository files navigation

Blog-backend-infrastructure

Description

This is the public infrastructure code of my blog (https://www.playingaws.com). This project is an example of a CDK project with 2 stacks: the infrastructure (backend) and the CICD to automate the infrastructure deployment.

Technology used

CDK project using TypeScript. It contains 2 stacks:

  • Backend: This stack contains the infrastructure to be deployed
    • API Gateway using a custom domain (api.playingaws.com)
    • Lambda: subscription and contact
    • DynamoDB
    • CloudWatch: lambda alarm and dashboard of lambdas
  • CICD: This stack contains the automation of the backend infrastructure
    • CodePipeline with 2 stages
      • source: CodeCommit with GitHub
      • build: CodeBuild (executes terraform apply)
    • S3 (as artifactory in CodePipeline)

Architecture diagram - autogenerated with cdk-dia

  • npm run dia
  • architecture-diagram

Type

Public code

More information

Review QA

npm run lint && npm run test

Test with SAM

  1. Download and run docker image of dynamodb-local
docker pull amazon/dynamodb-local
docker run -p 8000:8000 amazon/dynamodb-local
  1. Create dynamodb-local tables - review the template.yaml because the ID is going to change
aws dynamodb create-table --table-name bloginfrastructuresubscriptiondbF44CF6DE --attribute-definitions AttributeName=email,AttributeType=S --key-schema AttributeName=email,KeyType=HASH --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 --endpoint-url http://localhost:8000
aws dynamodb create-table --table-name bloginfrastructurecontactdbA1E13C61 --attribute-definitions AttributeName=name,AttributeType=S --key-schema AttributeName=name,KeyType=HASH --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 --endpoint-url http://localhost:8000
  1. Update lambda code to change the endpoint:
const dbClient = process.env.AWS_SAM_LOCAL ? new DynamoDBClient({ endpoint: 'http://docker.for.mac.localhost:8000/', }) : new DynamoDBClient();
  1. Generate the YAML file that SAM needs
cdk synth blog-backend-infrastructure --no-staging > template.yml
  1. Run SAM tests
sam local invoke blog-backend-infrastructure-contact-lbd -e tests/events/contact.json
sam local invoke blog-backend-infrastructure-subscription-lbd -e tests/events/subscription.json
  1. Review database records
aws dynamodb scan --table-name bloginfrastructuresubscriptiondbF44CF6DE --endpoint-url http://localhost:8000
aws dynamodb scan --table-name bloginfrastructurecontactdbA1E13C61 --endpoint-url http://localhost:8000

Additional links

  • How to create infrastructure with CDK here.
  • How to add CI/CD to my CDK project here.
  • How to create serverless applications with CDK and SAM here
  • How I decided on the technology behind the blog here.
  • API CDK v2 TypeScript: here

About

The public infrastructure code of my blog: https://playingaws.com

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published