Bilal Yousaf
Bilal Yousaf

Reputation: 484

How to use multiple sam templates in codebuild buildspec and codedeploy for lambda function deployment

I would like to use multiple SAM template files in my codebuild buildspec and in codedeploy for my lambda function deployment.

I have defined the multiple SAM template in my buildspec which builds without any errors. However, CodeDeploy only deploys one of the SAM templates.

How can i set up the codebuild and codedeploy to use multiple sam templates lambda function deployment?

Below is my Codebuild spec file that i have set up

version: 0.1
env:
  parameter-store:
    S3_BUCKET: "my_s3_bucket"

phases:
  install:
    commands:
      - aws cloudformation package --template-file samTemplate.yaml --s3-bucket $S3_BUCKET --output-template-file outputSamTemplate.yaml
      - aws cloudformation package --template-file samTemplatetwo.yaml --s3-bucket $S3_BUCKET --output-template-file outputSamTemplate.yaml


artifacts:
  type: zip
  files:
    - samTemplate.yaml
    - samTemplatetwo.yaml
    - outputSamTemplate.yaml

Below is my configuration for my codedeploy for deployment of lambda function. which creates the cloud formation stack.

CodeDeploy

Below are my sam templates that I am using. template 1

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: Lambda Functions Deployment 

Parameters:
  Environments:
    Type: String
    Default: UAT
    Description: Parameters  deployment Environments

Mappings:
  StagesMap:
    UAT:
      CONFIGBUCKET: staging-bucket-name
      CONFIGKEY: source-data-key-path
      URL: UAT
      RoleEnv: arn:aws:iam::

    NonProd:
      CONFIGBUCKET: non-prod-bucket-name
      CONFIGKEY: source-data-key-path
      RoleEnv: arn:aws:iam::

      
Resources:
  CDDemoLambda:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: lambda_function.lambda_handler
      Runtime: python3.6
      CodeUri: ./LambdaCode
      FunctionName: CDDemoLambda
      Description: 'Lambda function for CD Demo'
      MemorySize: 128
      Timeout: 30
      Role: !FindInMap
            - StagesMap
            - Ref: Environments
            - RoleEnv
      Environment:
        Variables:
          CONFIG_BUCKET: !FindInMap
            - StagesMap
            - Ref: Environments
            - CONFIGBUCKET
          CONFIG_KEY: !FindInMap
            - StagesMap
            - Ref: Environments
            - CONFIGKEY
    
  LambdaCopy:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: lambda_function.lambda_handler
      Runtime: python3.6
      CodeUri: ./LambdaCopy-CICD
      FunctionName: LambdaCopy-CICD
      Description: 'Lambda function to copy from one folder to another'
      MemorySize: 128
      Timeout: 30
      Role: !FindInMap
            - StagesMap
            - Ref: Environments
            - RoleEnv
      Environment:
        Variables:
          CONFIG_BUCKET: !FindInMap
            - StagesMap
            - Ref: Environments
            - CONFIGBUCKET
          CONFIG_KEY: !FindInMap
            - StagesMap
            - Ref: Environments
            - CONFIGKEY

template 2

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: Lambda Functions Deployment 

Parameters:
  Environments:
    Type: String
    Default: UAT
    Description: Parameters  deployment Environments

Mappings:
  StagesMap:
    UAT:
      CONFIGBUCKET: staging-bucket-name
      CONFIGKEY: source-data-key-path
      URL: UAT
      RoleEnv: arn:aws:iam::

    NonProd:
      CONFIGBUCKET: non-prod-bucket-name
      CONFIGKEY: source-data-key-path
      RoleEnv: arn:aws:iam::

      

Resources:
  Demo:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: lambda_function.lambda_handler
      Runtime: python3.6
      CodeUri: ./LambdaCode2
      FunctionName: Demo
      Description: 'Lambda func'
      MemorySize: 128
      Timeout: 30
      Role: !FindInMap
            - StagesMap
            - Ref: Environments
            - RoleEnv
      Environment:
        Variables:
          CONFIG_BUCKET: !FindInMap
            - StagesMap
            - Ref: Environments
            - CONFIGBUCKET
          CONFIG_KEY: !FindInMap
            - StagesMap
            - Ref: Environments
            - CONFIGKEY

Upvotes: 0

Views: 1000

Answers (1)

Robert Kossendey
Robert Kossendey

Reputation: 7028

One solution would be to look into nested stacks. This way you can create a third SAM template which has both of your templates as nested stacks. You then only have to deploy the parent template, and both of your templates will be deployed as separate stacks.

More information on that here.

Upvotes: 1

Related Questions