Prefixing resource names with stack names (dev, prod etc) in the SAM template

0

I am building a sam app with a two stage pipeline as detailed here https://catalog.workshops.aws/complete-aws-sam/en-US/module-4-cicd/ This question is related to another question I asked sometime back If I use hard coded names for resources like dynamo DB, I run into issues because the names should be unique but the dev and prod stages will end up trying to use the same hardcoded name then I end up with the error. To avoid this I just don't use any hardcoded names and let AWS generate its own random names which are almost guaranteed to be unique.
That works but it would be nice if I can name my dynamo db etc. with meaningful names with the stack name as a suffix - for example, my_dynamo_db_table_dev and my_dynamo_db_table_prod. Is there a way to modify the SAM template to use the stack names? By the way I am using code commit to automatically trigger the deployment through code pipeline so I don't use AWS cli manually to do the deployment so I won't be able to pass any command line arguments to sam deploy. Is there a way to achieve what I want?

2 Answers
0

Hello.

I think you are probably running sam deploy within CodeBuild, but how about specifying the environment with "--config-env" as an option for sam deploy?
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/deploying-using-codepipeline.html

Parameters can also be separated by separating the environments with "--config-env".
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html#serverless-sam-cli-config-basics

profile picture
EXPERT
answered a month ago
profile picture
EXPERT
reviewed a month ago
  • Thanks Riku, Yes I am running sam deploy with codebuild, and I guess the approach you suggested requires manually editing the buildspec_deploy.yml? At first I thought the buildspec files are autogenerated based on the contents of the codepipeline.yaml so should not be modified manually as they would be overwritten next time. But now I can see the codepiple.yaml actually refers to the the buildspec file so I guess I was wrong about this. So please can you confirm, is it ok to manully edit the buildspec files to pass additional parameters when using it alongside codebuild, codepipeline.yaml etc? Also can you post an example to show how to let it pick up the stack name automatically as a parameter? I have dev and prod stacks and I guess I will have to access the stack name from without the codepipeline.yaml so that I can specify the stack name as a suffix to resource names (like the name of the dynamo db table)

  • It is possible to set environment variables in buildspec.yml. I think you can use this function to change the settings for each environment. In other words, I thought it would be a good idea to create a CodePipeline for each environment and create a branch for each environment. https://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html#build-spec.env https://docs.aws.amazon.com/codebuild/latest/userguide/change-project-console.html#change-project-console-environment

  • template.yml

    AWSTemplateFormatVersion: "2010-09-09"
    Transform: AWS::Serverless-2016-10-31
    Description: test
    
    Parameters:
      Environment:
        Type: String
        AllowedValues:
          - dev
          - prod
    
    Resources:
      table:
        Type: AWS::DynamoDB::Table
        Properties:
          TableName: !Sub ${Environment}-table-name
          AttributeDefinitions:
            - AttributeName: userId
              AttributeType: S
          KeySchema:
            - AttributeName: userId
              KeyType: HASH
          BillingMode: PAY_PER_REQUEST
    

    samconfig.toml

    version = 0.1
    
    [dev.deploy.parameters]
    debug = true
    stack_name = "stack-dev-dynamodb"
    s3_bucket = "s3-dev-name"
    region = "ap-northeast-1"
    confirm_changeset = true
    disable_rollback = false
    parameter_overrides = [
      "Environment=dev"
    ]
    
    [prod.deploy.parameters]
    debug = true
    stack_name = "stack-prod-dynamodb"
    s3_bucket = "s3-prod-name"
    region = "ap-northeast-1"
    confirm_changeset = true
    disable_rollback = false
    parameter_overrides = [
      "Environment=prod"
    ]
    
0

Thanks Riku, at the moment I am using a single two-stage pipeline created by the template as described in the workshop I understand it is possible to do this if I create a separate pipeline for each environment but I just wanted to check if there is any way to achieve this without having to create a new pipeline? Using the existing two-stage pipeline? Is that not possible?

answered a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions