1. Answers
  2. Using aws ec2 with ecr

How do I use AWS EC2 with ECR?

To use AWS EC2 with ECR (Elastic Container Registry), we’ll create a Pulumi program that provisions an EC2 instance and an ECR repository. The EC2 instance will be configured to pull a Docker image from the ECR repository.

Steps:

  1. Create an ECR Repository: This is where your Docker images will be stored.
  2. Create an EC2 Key Pair: This will be used to access your EC2 instance.
  3. Create a Security Group: This will define the network access rules for your EC2 instance.
  4. Create an EC2 Instance: This instance will be configured to pull an image from the ECR repository.

Code Explanation:

  • ECR Repository: We’ll create an ECR repository to store Docker images.
  • EC2 Key Pair: We’ll create a key pair to SSH into the EC2 instance.
  • Security Group: We’ll create a security group to allow SSH access.
  • EC2 Instance: We’ll launch an EC2 instance and configure it to pull a Docker image from the ECR repository.

Here is the Pulumi program:

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

// Create an ECR repository
const ecrRepo = new aws.ecr.Repository("my-ecr-repo", {
    name: "my-ecr-repo",
});

// Create an EC2 key pair
const keyPair = new aws.ec2.KeyPair("my-key-pair", {
    keyName: "my-key-pair",
    publicKey: "<YOUR_PUBLIC_KEY>",
});

// Create a security group for the EC2 instance
const securityGroup = new aws.ec2.SecurityGroup("web-secgrp", {
    description: "Enable SSH access",
    ingress: [
        {
            protocol: "tcp",
            fromPort: 22,
            toPort: 22,
            cidrBlocks: ["0.0.0.0/0"],
        },
    ],
});

// Create an EC2 instance
const ec2Instance = new aws.ec2.Instance("web-server-www", {
    instanceType: "t2.micro",
    ami: "ami-0c55b159cbfafe1f0", // Amazon Linux 2 AMI
    keyName: keyPair.keyName,
    vpcSecurityGroupIds: [securityGroup.id],
    userData: pulumi.interpolate`#!/bin/bash
echo "Logging in to ECR"
$(aws ecr get-login --no-include-email --region ${aws.config.region})
docker pull ${ecrRepo.repositoryUrl}:latest
docker run -d -p 80:80 ${ecrRepo.repositoryUrl}:latest
`,
    tags: {
        Name: "web-server-www",
    },
});

// Export the public IP of the instance
export const publicIp = ec2Instance.publicIp;
export const ecrRepoUrl = ecrRepo.repositoryUrl;

Explanation:

  1. ECR Repository: We create an ECR repository named my-ecr-repo to store Docker images.
  2. EC2 Key Pair: We create a key pair named my-key-pair. Replace <YOUR_PUBLIC_KEY> with your actual public key.
  3. Security Group: We create a security group allowing SSH access on port 22 from any IP address.
  4. EC2 Instance: We create an EC2 instance using the Amazon Linux 2 AMI. The instance is configured to log in to the ECR repository, pull the latest Docker image, and run it.

Usage:

  • Public IP: The public IP of the EC2 instance will be exported, allowing you to SSH into it.
  • ECR Repository URL: The URL of the ECR repository will be exported, so you can push Docker images to it.

This program sets up an EC2 instance that pulls and runs a Docker image from an ECR repository. You can modify the userData script to customize the Docker container’s behavior.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up