Infrastructure As Code
AWS SAM

Provision DynamoDB Via SAM CFN

SAM templates stand out as a superior choice compared to traditional CFN for several key reasons:

  • Provides higher-level abstraction designed for serverless applications
  • Offers simplified packaging and deployment capabilities
  • Features a more concise and streamlined syntax compared to traditional CFN templates

DyanmoDB SAM

SAM templates excel in providing a focused and succinct representation of essential resources. By emphasizing only what's needed for your application, the templates enhance the clarity and manageability of your infrastructure setup.

Here's a quick overview of the workflow for provisioning DynamoDB using SAM templates:

StepDescription
BuildBuild your SAM application.
PackagePackage your application and dependencies.
DeployDeploy the application and provision DynamoDB.

For further insights, you can refer to the related commit. (opens in a new tab)

DynamoDB CFN Template

  • Open the AWS SAM template file template.yaml
  • Configure and develop the template as per your requirements.

The provided will deploy DynamoDB Table and DynamoDB Stream straight to the console.

Building

$ ./ddb/build 
/workspace/aws-cloud-project-bootcamp/ddb/template.yaml is a valid SAM Template. This is according to basic SAM Validation, for additional validation, please run with "--lint" option
== build
Starting Build inside a container                                                                                                       
Building codeuri: cruddur-messaging-stream runtime: python3.9 metadata: {} architecture: x86_64 functions: ProcessDynamoDBStream        
 
Fetching public.ecr.aws/sam/build-python3.9:latest-x86_64 Docker container image.............................................................................................................................................................................................................................................................................................................................................
Mounting /workspace/aws-cloud-project-bootcamp/ddb/cruddur-messaging-stream/cruddur-messaging-stream as /tmp/samcli/source:ro,delegated,
inside runtime container                                                                                                                
 
Build Succeeded

Package

$ ./ddb/package 
== package
        Uploading to ddb/d41d8cd98f00b204e9800998ecf8427e  22 / 22  (100.00%)
 
Successfully packaged artifacts and wrote output template to file /workspace/aws-cloud-project-bootcamp/.aws-sam/build/packaged.yaml.
Execute the following command to deploy the packaged template
sam deploy --template-file /workspace/aws-cloud-project-bootcamp/.aws-sam/build/packaged.yaml --stack-name <YOUR STACK NAME>

PS: The environment variables transfer was executed using this toml file.

Deploy

OperationLogicalResourceIdResourceTypeReplacement
+ AddDynamoDBTableAWS::DynamoDB::TableN/A
+ AddExecutionRoleAWS::IAM::RoleN/A
+ AddLambdaLogGroupAWS::Logs::LogGroupN/A
+ AddLambdaLogStreamAWS::Logs::LogStreamN/A
+ AddProcessDynamoDBStreamStreamAWS::Lambda::EventSourceMappingN/A
+ AddProcessDynamoDBStreamAWS::Lambda::FunctionN/A

Changeset created successfully.

arn:aws:cloudformation:<REGION>:<AWS-ID>:changeSet/samcli-deploy1685219291/550c1528-974d-4709-8c6a-f769fb05444f

Tom's Obvious, Minimal Language Files

TOML is a config file to read and write to store settings and parameters for our scripts and the actual template.

BenefitsUsage in IaC
Provides a structured and readable way to specify configuration parameters for IaC templates and scripts.Define variables and values in a separate file for easy modification and reuse of configurations.
Enables passing environment variables between the IaC template and accompanying scripts.Access values specified in TOML files within IaC templates or scripts using the appropriate parser or library.
Simplifies the management of configurations in IaC workflows.Use TOML files to define parameters passed to CloudFormation stacks for dynamic and flexible deployments.

Who is Tom?

Tom Preston-Werner is the creator of Jekyll and the founder ex-CEO of GitHub

Working with TOML

Get to work seamlessly reference the specified environment variable in your code.

  1. Sample CloudFormation artifact store:
      ArtifactStore:
        Location: "yacrud-codepipe-artifact"
        Type: S3
  1. Specify the environment variable in TOML:
[parameters]
ArtifactBucketName = "yacrud-codepipe-artifact"
  1. Reference the environment variable in your infra as code:
      ArtifactStore:
        Location: !Ref ArtifactBucketName
        Type: S3
  • CFN Template: This template contains the necessary configurations for the CI/CD deployment.
  • TOML file: This file includes specific settings and parameters required for the CI/CD process shipped to the console.

The instructions provided above are part of a CI/CD CloudFormation deployment process, which includes the above components.