3

I'm trying to "deploy" the content of a CodeCommit repository to a Lambda function (not an application). In this particular case it's a simple copy/paste from source to destination.

I'm struggling to find a solution that doesn't involve setting up another Lambda function. From what I understand, there is a solution using CodeBuild and CloudFormation.

Does anyone have a solution for this? Alternatively, can you point to any good documentation?

P.S:

I found this question that seems to answer my question but the links in the relevant answer are outdated.

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
Jarred
  • 71
  • 1
  • 9

2 Answers2

4

You can build a Code Commit Pipeline with a CodeBuild Job where you CodeCommit repository has a SAM Template like below and you run

sam build && sam deploy

From the codebuild job.


AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: A sample SAM template for deploying Lambda functions.

Resources:
# Details about the myDateTimeFunction Lambda function
  myDateTimeFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: myDateTimeFunction.handler
      Runtime: nodejs12.x
# Creates an alias named "live" for the function, and automatically publishes when you update the function.
      AutoPublishAlias: live
      DeploymentPreference:
# Specifies the deployment configuration
          Type: Linear10PercentEvery2Minutes

This documentation page describes the same CodeCommit Rolling deployments for Lambda functions

samtoddler
  • 8,463
  • 2
  • 26
  • 21
  • Thanks for the help, it already makes it clearer ! Could you provide more info regarding the buildspec.yml file content to run "sam build & sam deploy" ? If I understand this correctly, I should add two files to codecommit : - buildspec.yml to trigger the sam template - sam.yml which contains something like the code you gave Is that correct ? – Jarred Jan 24 '21 at 19:02
  • @Jarred correct, when you configure your ```codebuild``` job you just have the command ```sam build && sam deploy``` , give you have [aws sam](https://aws.amazon.com/serverless/sam/) cli installed on the box for codebuild and the ```IAM Role``` for the ```codebuild``` has necessary permissions. – samtoddler Jan 24 '21 at 19:11
2

This is the solution that worked for me.

I setup a pipeline with CodeCommit as source and a Build Phase (no Deploy Phase).

The Build phase reads a buildspec.yml file which itself reads SAM template called template.yml. The SAM stack is created via CloudFormation.

I created an s3 bucket to hold the build artifacts.

Here is the sample buildspec.yml file:

version: 0.2

phases:
  install:
    commands:
      - echo Nothing to do in the install phase...
  pre_build:
    commands:
      - echo Nothing to do in the pre_build phase...
  build:
    commands:
    - aws cloudformation package --template-file template.yml 
                                  --s3-bucket <bucketname>
                                  --output-template-file newtemplate.yml
    - aws cloudformation deploy --stack-name <stackname>
                                --capabilities CAPABILITY_IAM
                                --template-file newtemplate.yml
                                --role-arn arn:aws:iam::<account number>:role/CloudFormationServiceRole
  post_build:
    commands:
      - echo Build completed

Here is the sample template.yml file :

AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: CloudFormation Stack for the lambda function

Resources:
# Details about the Lambda function
  <StackName>:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs12.x
      CodeUri: src/
# Creates an alias named "live" for the function, and automatically publishes when you update the function.
      AutoPublishAlias: live
      DeploymentPreference:
# Specifies the deployment configuration
          Type: AllAtOnce

The file structure is :

.
├── src/
│   ├── node_modules/
│   └── index.js
├── builspec.yml
└── template.yml

Make sure you set the correct IAM policies for the CloudFormation and CodeBuild IAMs.

Jarred
  • 71
  • 1
  • 9