Categories
API Gateway AWS CloudFormation CodeBuild CodePipeline Lambda S3

CI/CD for static assets and Lambda function

This Gist is based on Building a continuous delivery pipeline for a Lambda application with AWS CodePipeline.

What happened?

Needed to build a NodeJS project, upload the static assets to an S3 bucket and also deploy a small Lambda function that lived in a subfolder.

Prerequisites

The AWS Guide above will get you familiarized with the setup, but there are a couple of changes required on both the buildspec.yml and template.yml files.

buildspec.yml

You start at the root of your repo…which is nice.

Pre-build and build commands a pretty much self-explanatory. For whatever reason the index.js, template.yml and outputtemplate.yml needed to be by themselves in a folder.

I had initially configured an Action in the Deploy stage that published the static assets to a static website hosting S3 Bucket. I removed it and used the aws s3 sync command instead.

version: 0.2

phases:
  pre_build:
    commands:
      - cd lambda-function/
      - npm install
      - cd ..
  build:
    commands:
      - npm install && npm run build
      - aws cloudformation package --template-file lambda-function/template.yml --s3-bucket codepipeline-S3_BUCKET --output-template-file lambda-function/outputtemplate.yml --kms-key-id ID
  post_build:
    commands:
      - cd build/
      - aws s3 sync . s3://static-assets-bucket/
artifacts:
  type: zip
  files:
    - lambda-function/template.yml
    - lambda-function/outputtemplate.yml
  discard-paths: yes

https://gist.github.com/miligraf/16c4b42feb73e3e2c9a8377ec35b67fd#file-buildspec-yml

template.yml

Nothing out of the ordinary really.

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: My Lambda Function
Resources:
  MyLambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs12.x
      CodeUri: ./
      Environment:
        Variables:
          AUTH_TOKEN: VAR
      Events:
        AuthProxyApi:
          Type: HttpApi
          Properties:
            Path: /
            Method: POST

https://gist.github.com/miligraf/16c4b42feb73e3e2c9a8377ec35b67fd#file-template-yml

Leave a Reply