Showing posts with label #Cicd. Show all posts
Showing posts with label #Cicd. Show all posts

Sunday, December 8, 2024

Building a Serverless REST API with AWS Lambda and API Gateway

Building a Serverless REST API with AWS Lambda and API Gateway

In the modern development landscape, serverless architecture has gained immense popularity due to its cost efficiency, scalability, and ease of use. AWS Lambda and API Gateway form a powerful duo for creating serverless REST APIs. This blog will guide you through creating a simple CRUD API using AWS Lambda, API Gateway, and Node.js with TypeScript.

Saturday, November 2, 2024

Building Docker Images in AWS CodeBuild and Storing them in ECR using CodePipeline

Building Docker Images in AWS CodeBuild and Storing them in ECR using CodePipeline

Introduction

As cloud-native applications become the standard, serverless and containerized solutions have surged in popularity. For developers working with AWS, using Docker and AWS CodePipeline provides a streamlined way to create, test, and deploy applications. In this blog, we’ll discuss how to automate Docker image builds in AWS CodeBuild, set up a CI/CD pipeline using AWS CodePipeline, and push the final image to Amazon Elastic Container Registry (ECR) for storage.

Image

This guide is suitable for AWS intermediate users who are new to Docker and are interested in building robust CI/CD pipelines.

Step 1: Setting Up an Amazon ECR Repository

Amazon Elastic Container Registry (ECR) is a fully managed Docker container registry that helps you securely store, manage, and deploy Docker container images. 

Let’s start by creating an ECR repository

  1. Log in to the AWS Management Console.
  2. Navigate to Amazon ECR and click Create repository.
  3. Provide a name for your repository, e.g., my-docker-application-repo.
  4. Configure any additional settings as needed.
  5. Click Create repository.

Once created, ECR will provide you with a repository URL that will be used to push and pull Docker images.

Step 2: Preparing Your Docker Application

You should have a Dockerfile prepared for your application. The Dockerfile is a script with instructions on how to build your Docker image. Here’s an example of a simple Dockerfile:

        # Use an official node image as the base
        FROM node:14

        # Create and set the working directory
        WORKDIR /usr/src/app

        # Copy application code
        COPY . .

        # Install dependencies
        RUN npm install

        # Expose the application port
        EXPOSE 8080

        # Run the application
        CMD ["npm", "start"]

Place this Dockerfile in the root directory of your project.

Step 3: Creating the CodeBuild Project for Docker Image Creation

AWS CodeBuild will be responsible for building the Docker image and pushing it to ECR. Here’s how to set it up:

Create a CodeBuild Project

  1. In the AWS Management Console, navigate to AWS CodeBuild.
  2. Click Create build project.
  3. Name your project, e.g., Build-Docker-Image.
  4. Under Source, select your source repository, such as GitHub or CodeCommit, and provide the repository details.
  5. Under Environment, select the following:
    1. Environment image: Choose Managed image.
    2. Operating system: Amazon Linux 2
    3. Runtime: Standard
    4. Image: Select a Docker-enabled image, such as aws/codebuild/amazonlinux2-x86_64-standard:3.0
    5. Privileged: Enable privileged mode to allow Docker commands in the build.
  6. Under Buildspec, you can either define the commands directly or use a buildspec.yml file in your source code repository. For this example, we’ll use a buildspec.yml.

Creating the buildspec.yml File

In the root directory of your project, create a buildspec.yml file with the following contents:
    version: 0.2

    phases:
      pre_build:
        commands:
          - echo Logging in to Amazon ECR...
          - aws ecr get-login-password --region  | docker login --username AWS --password-stdin 
      build:
        commands:
          - echo Building the Docker image...
          - docker build -t my-application .
          - docker tag my-application:latest :latest
      post_build:
        commands:
          - echo Pushing the Docker image to ECR...
          - docker push :latest
    artifacts:
      files:
        - '**/*'
Replace <your-region> and <your-ecr-repo-url> with the actual values for your AWS region and ECR repository URL.

Step 4: Setting Up AWS CodePipeline

Now that CodeBuild is ready to build and push your Docker image, we’ll set up AWS CodePipeline to automate the build process.

Create a CodePipeline

  1. Go to AWS CodePipeline and click Create pipeline.
  2. Name the pipeline, e.g., Docker-Build-Pipeline.
  3. Choose a new or existing S3 bucket for pipeline artifacts.
  4. In Service role, select "Create a new service role."
  5. Click Next.

Define Source Stage

  1. For Source provider, select your code repository (e.g., GitHub).
  2. Connect your repository and select the branch containing the Dockerfile and buildspec.yml.
  3. Click Next.

Add Build Stage

  1. In the Build provider section, select AWS CodeBuild.
  2. Choose the CodeBuild project you created earlier, Build-Docker-Image.
  3. Click Next.

Review and Create Pipeline

Review your settings, and then click Create pipeline. Your pipeline is now set up to build the Docker image and push it to ECR whenever changes are detected in the source repository.

Step 5: Setting Up IAM Permissions

For security purposes, AWS IAM policies need to be configured correctly to enable CodeBuild and CodePipeline to access ECR. Here’s how to configure permissions:

  1. CodeBuild Service Role: Ensure the role used by CodeBuild has permissions for ECR.
  2. CodePipeline Service Role: The CodePipeline service role should have the necessary permissions to trigger CodeBuild and access the repository.
Example IAM Policy for CodeBuild:
       {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Action": [
                "ecr:GetAuthorizationToken",
                "ecr:BatchCheckLayerAvailability",
                "ecr:PutImage",
                "ecr:InitiateLayerUpload",
                "ecr:UploadLayerPart",
                "ecr:CompleteLayerUpload"
              ],
              "Resource": "*"
            },
            {
              "Effect": "Allow",
              "Action": [
                "s3:GetObject",
                "s3:PutObject",
                "s3:ListBucket"
              ],
              "Resource": "*"
            }
          ]
        }

Step 6: Testing the Pipeline

With everything in place, push some changes to your source repository. CodePipeline should automatically detect the changes, trigger CodeBuild, and build and push the Docker image to ECR.

You can verify this by checking the CodePipeline console to see each stage’s status. If everything succeeds, your Docker image will be available in Amazon ECR!

Conclusion

In this blog, we explored how to build a Docker image in AWS CodeBuild and push it to Amazon ECR, all within an automated pipeline set up using AWS CodePipeline. By using these services together, you can create a scalable, efficient, and reliable CI/CD pipeline for containerized applications, without the need for managing server infrastructure.

This approach leverages the benefits of serverless infrastructure and allows you to focus more on building and deploying applications rather than managing build servers.



Sunday, December 3, 2023

Installing Node.Js 18 in AWS CodeBuild using the Ubuntu build image

Installing Node.Js 18 in AWS CodeBuild using the Ubuntu build image

Node.Js and AWS CodeBuild

AWS CodeBuild is a fully managed build service that can compile source code, run tests, and produce software packages that are ready to deploy. It provides a wide range of pre-built build environments for popular programming languages, frameworks, and tools, including Node.js.

Node.js is a popular open-source JavaScript runtime built on Chrome’s V8 JavaScript engine. It allows developers to build fast, scalable, and highly performant server-side applications using JavaScript. The latest stable version of Node.js is 18.x, which was released on April 19, 2022.

Why Ubuntu not Amazon Linux 2 images?

Amazon Linux 2 is a Linux distribution that is designed to work seamlessly with AWS services. However, it is based on the CentOS/RHEL Linux distribution, which has a slower release cycle for new software versions compared to other Linux distributions. This means that Amazon Linux 2 may not have the latest version of some software packages, including Node.js.

In this article, we’ll learn how to install Node.js 18 with available images in AWS CodeBuild.

Step 1: Create a CodeBuild project

To get started, log in to the AWS Management Console and navigate to the AWS CodeBuild service. Click on the “Create project” button to create a new CodeBuild project.

Give your project a meaningful name, and choose the source code provider, source code location, and build environment. For this example, we’ll use the default “Ubuntu” environment with image identifier “aws/codebuild/standard:6.0”.

Step 2: Specify the build commands

In the “Buildspec” section of your CodeBuild project configuration, you can specify the build commands that CodeBuild should run. We’ll use the following commands to install Node.js 18:

version: 0.2
phases:
install:
commands:
- echo "Installing Node.js 18"
- n 18
build:
commands:
- echo "Build commands go here"

The commands section runs the echo command to indicate that we're installing Node.js 18 and then installs it using n 18 command.

You can replace the echo command in the build phase with your own build commands, such as npm install or yarn build.

Step 3: Start the build

Once you’ve specified the build commands in your CodeBuild project, you can start a new build by clicking on the “Start build” button. CodeBuild will spin up a new instance of the build environment, install Node.js 18, and run the build commands.

After the build completes, you can view the build logs to see the output of the build commands. If there were any errors or warnings, you can use the logs to diagnose the issue and make any necessary changes to your build commands.

Conclusion

In this article, we learned how to install Node.js 18 with available images in AWS CodeBuild. By using the install phase in your buildspec, you can easily install any version of Node.js that is available in the CodeBuild environment.

While Amazon Linux 2 does not currently support Node.js 18, we can still use AWS CodeBuild to install Node.js 18 by using a different build image. By following the steps outlined above, you can easily install Node.js 18 in AWS CodeBuild and use it to build and test your Node.js applications. With Node.js 18, you can take advantage of its new features and improvements to build faster and more efficient applications.