DEV Community

Lasantha Sanjeewa Silva
Lasantha Sanjeewa Silva

Posted on • Updated on

AWS Lambda CRUD API Create & Setup AWS CodePipeline

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

Image description

You can use the sample code repository to get code content. Gitlab Repository URL.

Image description

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

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

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

  • 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

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

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

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.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

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 -

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 -

Image description

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

Image description

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

Image description

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

Image description

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

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

Image description

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

Image description

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

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

Image description

Congratulations, Your API is Up and Running.

Image description

Otherwise, you can use Postman to test API calls.
Download Postman -

Next, go to DynamoDB and in Explore items add the following values.
Image description

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

Image description

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.

Top comments (0)