Create ECR, Build Image, and Push Image

Create ECR, Build Image, and Push Image

In this step, we will create an ECR, build an image, and push it to the ECR for use in Lambda creation.

  1. Access the AWS console
    • Search for ECR.
    • Select ECR.

ECR

  1. On the ECR page, click Create.

ECR

  1. On the Create Repository page:
    • In the Visibility settings:

      • Private: If you want your repository to be private.
      • Public: If you want your repository to be public.
    • In the Repository name field, enter the name of the repository you want; here, we’ll enter django-serverless-demo.

    • In the (Optional) Image scan settings:

      • This option allows you to enable image scanning to check for potential security vulnerabilities in the images you push to the repository. If enabled, the system will automatically scan the images and provide a report on any detected vulnerabilities. This is very useful if you want to ensure the safety of the images before using them in production.
    • In the (Optional) Encryption settings:

      • This option allows you to enable encryption for the images stored in the repository. When encryption is enabled, all images will be encrypted before being stored, protecting data from unauthorized access. You can use AWS-managed encryption keys or manage your encryption keys for higher security control.
    • In the (Optional) Tag Immutability:

      • This option allows you to enable tag immutability. When this feature is enabled, once a tag is assigned to an image, it cannot be changed or reassigned to another image. This ensures the integrity of image versions, avoiding accidental overwriting or changes to important tagged images.
    • After making your selections, click Create Repository.

ECR ECR

  1. Click on the repository you just created.
    • Select View push commands to see how to push the image to the repository.

ECR ECR ECR

  1. Open Terminal in your source code directory.
  • First, log in to your repository:
aws ecr get-login-password --region ap-southeast-1 | docker login --username AWS --password-stdin <account-id>.dkr.ecr.ap-southeast-1.amazonaws.com
  • Next, build the image using the following command. For Mac M1 users who want to run the x86_64 architecture, add –platform linux/amd64:
docker build --platform linux/amd64 -t django-serverless-demo -f docker/DockerfileLambda .
  • Finally, tag the image and push it to the ECR:
docker tag django-serverless-demo:latest <acoount-id>.dkr.ecr.ap-southeast-1.amazonaws.com/django-serverless-demo:latest
docker push <account-id>.dkr.ecr.ap-southeast-1.amazonaws.com/django-serverless-demo:latest  

ECR

Next, we will create Lambda & API Gateway.