├── .gitignore ├── Jenkinsfile ├── README.md ├── main.go ├── main.tf ├── main_test.go ├── schema.png └── variables.tf /.gitignore: -------------------------------------------------------------------------------- 1 | .terraform 2 | *.zip 3 | *.tfstate 4 | *.tfstate.backup 5 | main -------------------------------------------------------------------------------- /Jenkinsfile: -------------------------------------------------------------------------------- 1 | def bucket = 'deployment-packages-mlabouardy' 2 | def functionName = 'Fibonacci' 3 | def region = 'eu-west-3' 4 | 5 | node('slaves'){ 6 | stage('Checkout'){ 7 | checkout scm 8 | } 9 | 10 | stage('Test'){ 11 | sh 'go get -u github.com/golang/lint/golint' 12 | sh 'go get -t ./...' 13 | //sh 'golint -set_exit_status' 14 | sh 'go vet .' 15 | sh 'go test .' 16 | } 17 | 18 | stage('Build'){ 19 | sh 'GOOS=linux go build -o main main.go' 20 | sh "zip ${commitID()}.zip main" 21 | } 22 | 23 | stage('Push'){ 24 | sh "aws s3 cp ${commitID()}.zip s3://${bucket}" 25 | } 26 | 27 | stage('Deploy'){ 28 | sh "aws lambda update-function-code --function-name ${functionName} \ 29 | --s3-bucket ${bucket} \ 30 | --s3-key ${commitID()}.zip \ 31 | --region ${region}" 32 | } 33 | 34 | if (env.BRANCH_NAME == 'master') { 35 | stage('Publish') { 36 | def lambdaVersion = sh( 37 | script: "aws lambda publish-version --function-name ${functionName} --region ${region} | jq -r '.Version'", 38 | returnStdout: true 39 | ) 40 | sh "aws lambda update-alias --function-name ${functionName} --name production --region ${region} --function-version ${lambdaVersion}" 41 | } 42 | } 43 | } 44 | 45 | def commitID() { 46 | sh 'git rev-parse HEAD > .git/commitID' 47 | def commitID = readFile('.git/commitID').trim() 48 | sh 'rm .git/commitID' 49 | commitID 50 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CI/CD for Lambda Functions with Jenkins 2 | 3 |

4 | 5 |

6 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "errors" 5 | 6 | "github.com/aws/aws-lambda-go/lambda" 7 | ) 8 | 9 | func fibonacci(n int) int { 10 | if n <= 1 { 11 | return n 12 | } 13 | return fibonacci(n-1) + fibonacci(n-2) 14 | } 15 | 16 | func handler(n int) (int, error) { 17 | if n < 0 { 18 | return -1, errors.New("Input must be a positive number") 19 | } 20 | return fibonacci(n), nil 21 | } 22 | 23 | func main() { 24 | lambda.Start(handler) 25 | } 26 | -------------------------------------------------------------------------------- /main.tf: -------------------------------------------------------------------------------- 1 | // Provider configuration 2 | provider "aws" { 3 | region = "${var.region}" 4 | } 5 | 6 | // S3 bucket 7 | resource "aws_s3_bucket" "bucket" { 8 | bucket = "${var.bucket}" 9 | acl = "private" 10 | } 11 | 12 | // Jenkins slave instance profile 13 | resource "aws_iam_instance_profile" "worker_profile" { 14 | name = "JenkinsWorkerProfile" 15 | role = "${aws_iam_role.worker_role.name}" 16 | } 17 | 18 | resource "aws_iam_role" "worker_role" { 19 | name = "JenkinsBuildRole" 20 | path = "/" 21 | 22 | assume_role_policy = <