Skip to content

Instantly share code, notes, and snippets.

@miligraf
Last active May 28, 2020 13:43
Show Gist options
  • Save miligraf/16c4b42feb73e3e2c9a8377ec35b67fd to your computer and use it in GitHub Desktop.
Save miligraf/16c4b42feb73e3e2c9a8377ec35b67fd to your computer and use it in GitHub Desktop.
CodePipeline + CodeBuild + Lambda

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.

template.yml

Nothing out of the ordinary really.

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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment