Deploy a Lambda function triggered by S3 event using CodePipeline 
Tags: aws, lambda, codepipeline
Published: 2020-04-03 14:31:22

This post demonstrates how to deploy a Lambda function hosted in the GitHub using CodePipeline. We use one of our projects [S3toDDB](https://github.com/vt-digital-libraries-platform/S3toDDB) as an example. The Lambda function in this project receives event notifications from a S3 bucket, process data in files, and create or update data in DynamoDB. Please see project [README](https://github.com/vt-digital-libraries-platform/S3toDDB) for details.    

Go to the [CodePipeline](https://console.aws.amazon.com/codesuite/codepipeline/pipelines?region=us-east-1) console and select “create pipeline”. 

<b>In the Step 1:</b>
1. Enter a pipeline name
2. Under service role, select new service role and enter a role name, click Next button.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7e117rhmyp4du3xlcipa.png)

<b>In the Step 2:</b>
1. Select GitHub as the source provider
2. Select target GitHub repository
3. Select target branch
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/o4oz9esjbdcc6cqhgjqt.png)

<b>In the step 3:</b>
1. Select AWS CodeBuild
2. Select create project
In the create build project popup window
1. Enter a project name
2. Select Ubuntu as the Operating system
3. Select "Standard"
4. Select "image:2.0"
5. select New service role and role name
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9vdl7ejodp1fh0tnxunn.png)
6. go to IAM console and add a ```PutObject``` permission to the S3 bucket as specified in the [buildspec.yml](https://github.com/vt-digital-libraries-platform/S3toDDB/blob/master/buildspec.yml#L11)
```
{
    "Effect": "Allow",
        "Resource": [
            "arn:aws:s3:::bucketname/*"
        ],
        "Action": [
            "s3:PutObject"
        ]
}
```

<b>In the step 4:</b>
1. Select AWS Cloudformation
2. Under the action mode, select “create or update a stack”
3. Enter a stack name
4. Under the Artifact name, select “BuildArctifact”. Enter “packaged-template.yml” in the File name
5. Under the Capabilities, select “CAPABILITY_AUTO_EXPAND” and “CAPABILITY_IAM”
6. Select a role name. (Note. You need to create a role with the necessary permissions)
7. Under the advanced, supply the [Parameter Override](https://github.com/vt-digital-libraries-platform/S3toDDB#codedeploy-parameter-override)
```
{"APPIMGROOTPATH":"https://img.cloud.lib.vt.edu/iawa/","BibliographicCitation":"Researchers wishing to cite this collection should include the following information: - Special Collections, Virginia Polytechnic Institute and State University, Blacksburg, Va.","CollectionCategory":"IAWA","DYNOCollectionTABLE":"Collection-xxxxxx","DYNOArchiveTABLE":"Archive-yyyyyy","NOIDNAA":"53696","NOIDScheme":"ark:/","NOIDTemplate":"eeddeede","REGION":"us-east-1","RightsHolder":"Special Collections, University Libraries, Virginia Tech","RightsStatement":"Permission to publish material from the must be obtained from University Libraries Special Collections, Virginia Tech.","S3BucketName":"iawa-s3csv","LambdaLayerParameter":"arn:aws:lambda:us-east-1:xxxxxxxxx:layer:iawa-layer:1"}
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ukqbjozwgibprh7f38tz.png)

That's it, click the next button and start the codepipeline. If everything works, it should show succeed in every stages. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gyw8m7wiakw0l023u7py.png)