AWS Lambda CRUD API Create & Setup AWS CodePipeline
Tags: aws, api, cloud, lambda
Published: 2022-02-26 16:38:00

Now we going to build CRUD API using some AWS services. First of all, we can go through the architectural diagram.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jwq2mzlmr0mor1bud2qq.png)

You can use the sample code repository to get code content. Gitlab Repository [URL](https://gitlab.com/lasantha96/http-crud-api).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9k67xywhduc83tohygbw.gif)

Firstly we want to create an Amazon DynamoDB table.

- Use table name as "http-crud"
- Type primary key as "id"
- Next click create button

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qnqoye0zyedkbjsab3sj.png)

Next, create the AWS Lambda function.

- Use function name as "http-crud-fun"
- Runtime info select Node.js 14.x
- Under Permissions choose Change default execution role
- Select Create a new role from AWS policy templates
- Type Role name as "http-crud-role"
- For Policy templates, choose **Simple microservice permissions**
  This policy grants the Lambda function permission to interact 
  with DynamoDB
- Choose Create function

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nknudpqotlr9gjp50sk3.png)

Create an HTTP API in Amazon API Gateway.

- Choose Create API
- For HTTP API choose Build
- Use API name as "http-crud-api"
- Choose Next. For Configure routes, choose Next to skip route 
  creation. You create routes later.Review the stage that API 
  Gateway creates for you ($default), and then choose Next.After 
  Choose Create.

Next, we going to Create routes in HTTP API.

- Choose your API (http-crud-api)
- On the left panel choose Routes
- Choose Create

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vhcc9t6hr85q95ztugw9.png)

- For Method, choose GET
- For the path, enter /books/{id}. The {id} at the end of the path 
  is a path parameter that API Gateway retrieves from the request 
  path when a client makes a request. Choose Create. Repeat steps 
  for GET /books, DELETE /books/{id}, and PUT /books

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/urww1wjmu7p3h7sw3if2.png)

After that Create integration in Amazon API Gateway.

- Choose your API (http-crud-api)
- Choose Integrations
- Choose Manage integrations and then choose Create
- Skip Attach this integration to a route. You complete that in a 
  later step. For Integration type, choose Lambda function. For 
  Lambda function, enter http-crud-fun
- Choose Create

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k07zb04nhs66zxz0nh4v.png)

Next, attach your integration to routes in Amazon API Gateway.

- Choose your API (http-crud-api)
- Choose Integrations
- Choose a route
- Under Choose an existing integration, choose http-crud-fun

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z6dxhfmtu3iis4xtzx7d.png)

Now that you have an HTTP API with routes and integrations, you can test your API after creating CI Pipeline.

## Now we create a GitLab repository and attached these files and push.

index.js  <- clone repo and copy index.js content.

buildspec.yml -> Next, we want to create "buildspec.yml" for the AWS CodeBuild setup. Create a buildspec.yml file and copy content. Change lambda function name inside buildspec.yml update-lambda section.

Push index.js & buildspec.yml to GitLab repository.

## Now we setup CI piepline.

Next, we want to create an IAM user for AWS CodeCommit.

- Go to IAM service and click create a user.
- Use username as "gitlab-user"
- Give this user to "AWSCodeCommitFullAccess" AWS managed policy
- next add inline policy for gitlab push mirroring. You can get 
  JSON policy sample in git [repo] 
  (https://gitlab.com/lasantha96/http-crud-api).In resource 
  section add your lambda function ARN.

Next, we want to generate Security Credentials "HTTPS Git credentials for AWS CodeCommit" for gitlab-user. Please consider notedown UserName & Password.

Next, create the AWS CodeCommit repository and mirror it with Gitlab repo.

Goto AWS CodeCommit & create repository following.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0egcde9dfa00ps6c1qsm.png)

After that Goto GitLab repo and Go setting -> Repository section. In the section click "Mirroring repositories".

Next copy your AWS CodeCommit repo HTTP URL.
Ex - https://git-codecommit.us-east-2.amazonaws.com/v1/repos/http-crud-api

After that go to the "Git repository URL" section and add the following URL. You can refer sample URL for creating your URL.
Sample URL - https://gitlab-user-at-932747549174@git-codecommit.us-east-2.amazonaws.com/v1/repos/http-crud-api

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o3jt7sr61o3bqdfu3cad.png)

After you can see the following. Your Codecommit has the same repository in GitLab.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mhec90tlz7uos98ygocy.png)

## Now we go to set up AWS CodeBuild and AWS CodePipeline.

**Code Build**

- Go to CodeBuild section and click create build project
- Give the name as "http-crud-api-build"
- In the source, section select AWS CodeCommit and select our repo 
  and branch
- Next goto environment section and select Operating system as 
  "Amzon Linux 2"
- Select Runtime(s) as Standard
- Select image as latest
- Keep other settings as default
- If you want you can enable/disable monitoring
- Next create build project button

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dnwtvxtxmeacopccxe2x.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vl4bt3ywoa0zcs1y1df4.png)

We want to add the following JSON policy for Codecommit created IAM Role. You can get content using this sample repo.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g78b20sg6x89iomg7qsg.png)

**AWS CodePipeline**

- Go to CodePipeline and select create pipeline
- Use name as "http-crud-api-pipeline" and other things keep 
  default & click next

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2yf3nd9y6tr7o8xssebc.png)

- Next select Source provide as CodeCommit & select our repo and 
  branch
- After that click next

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iv8ipbiapsyym76nd7oi.png)

- Next Build provider as CodeBuild and Select Project name as 
  "http-crud-api-build" and other things keep default and click 
  next

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u1hokey2jz6dwd7x97zy.png)

After that click skip deploy stage and click review. Finally, click creates a pipeline button.

Now you can see your newly created AWS Pipeline is running. Finally, you can check the Pipeline status following.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5oma5e87m09iuhispdj6.png)

Next, you can check the Lambda function and you can see your function is updated.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/78osek9f23jqlsjwq18h.png)

Congratulations, Your API is Up and Running.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/umxa71tna4im5ls6hwf4.png)

Otherwise, you can use Postman to test API calls.
Download Postman - https://www.postman.com/downloads/

Next, go to DynamoDB and in Explore items add the following values.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4qchzuai1oyz797ikk6l.jpg)

Copy API gateway Invoke URL and paste it, Postman, using API method.
Ex- GET Invoke URL/books

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/omonjdpqs1n9p726ub2u.png)

This setup running means you want to pay a lot of money. To prevent unnecessary costs you can remove the following service.

> DynamoDB Tables, HTTP API In API Gateway, Lambda function, Lambda function's log group, Lambda function's execution role, CodePipeline, CodeBuild and CodeCommit Repository.

**Thanks for reading the Article.**