Skip to content

Commit 2b89fb4

Browse files
committed
feat: use ec2 to upload
1 parent 5d63b17 commit 2b89fb4

File tree

4 files changed

+105
-11
lines changed

4 files changed

+105
-11
lines changed

tf-module/ds_img_to_ecr/ec2.tf

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
data "aws_iam_instance_profile" "ec2_profile" {
2+
name = var.ec2_profile_iam_name
3+
}
4+
data "aws_subnet" "ec2_subnet" {
5+
id = var.cumulus_subnet_id
6+
}
7+
8+
resource "aws_instance" "docker_builder" {
9+
subnet_id = data.aws_subnet.ec2_subnet.id
10+
security_groups = var.security_group_id
11+
ami = var.ami_id
12+
instance_type = var.instance_type
13+
key_name = var.key_name
14+
associate_public_ip_address = true
15+
iam_instance_profile = data.aws_iam_instance_profile.ec2_profile.name
16+
17+
user_data = <<-EOF
18+
#!/bin/bash
19+
set -e
20+
21+
# Install Docker
22+
yum update -y
23+
amazon-linux-extras enable docker
24+
yum install -y docker
25+
service docker start
26+
usermod -aG docker ec2-user
27+
28+
# Install AWS CLI (if not already installed)
29+
yum install -y aws-cli
30+
31+
# Login to ECR
32+
aws ecr get-login-password --region ${var.aws_region} | docker login --username AWS --password-stdin ${aws_ecr_repository.repo.repository_url}
33+
34+
# Pull the AMD64 image
35+
docker pull --platform=linux/amd64 ${var.github_image_url}:${var.image_tag}
36+
37+
# Tag and push the image to ECR
38+
docker tag ${var.github_image_url}:${var.image_tag} ${aws_ecr_repository.repo.repository_url}:${var.image_tag}
39+
docker push ${aws_ecr_repository.repo.repository_url}:${var.image_tag}
40+
41+
42+
TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`
43+
INSTANCE_ID=`curl http://169.254.169.254/latest/meta-data/instance-id -H "X-aws-ec2-metadata-token: $TOKEN"`
44+
aws ec2 terminate-instances --instance-ids $INSTANCE_ID --region ${var.aws_region}
45+
EOF
46+
47+
tags = {
48+
Name = "Docker-Build-Instance"
49+
}
50+
}
51+

tf-module/ds_img_to_ecr/ecr1.tf

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ output "ecr_repo_url" {
2424
value = aws_ecr_repository.repo.repository_url
2525
}
2626

27-
resource "null_resource" "docker_pull_push" {
28-
provisioner "local-exec" {
29-
command = <<EOT
30-
aws ecr get-login-password --region ${var.aws_region} | docker login --username AWS --password-stdin ${aws_ecr_repository.repo.repository_url}
31-
docker pull --platform=linux/amd64 ${var.github_image_url}:${var.image_tag}
32-
docker tag ${var.github_image_url}:${var.image_tag} ${aws_ecr_repository.repo.repository_url}:${var.image_tag}
33-
docker push ${aws_ecr_repository.repo.repository_url}:${var.image_tag}
34-
EOT
35-
}
36-
depends_on = [aws_ecr_repository.repo]
37-
}
27+
# Backup option to upload docker locally. But EC2 may be a more stable option...
28+
#resource "null_resource" "docker_pull_push" {
29+
# provisioner "local-exec" {
30+
# command = <<EOT
31+
# aws ecr get-login-password --region ${var.aws_region} | docker login --username AWS --password-stdin ${aws_ecr_repository.repo.repository_url}
32+
# docker pull --platform=linux/amd64 ${var.github_image_url}:${var.image_tag}
33+
# docker tag ${var.github_image_url}:${var.image_tag} ${aws_ecr_repository.repo.repository_url}:${var.image_tag}
34+
# docker push ${aws_ecr_repository.repo.repository_url}:${var.image_tag}
35+
# EOT
36+
# }
37+
# depends_on = [aws_ecr_repository.repo]
38+
#}

tf-module/ds_img_to_ecr/terraform.tfvars.example

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,11 @@ tags = {
1010
}
1111
aws_region = "us-west-2"
1212
image_tag="9.6.0"
13+
14+
ecr_repo_name = "test4"
15+
cumulus_lambda_vpc_id = "vpc-xxx"
16+
cumulus_subnet_id = "subnet-xxx"
17+
security_group_id = "sg-xxx"
18+
ami_id = "ami-xxx"
19+
key_name = "mcp-unity-xxx-1"
20+
ec2_profile_iam_name = "uds-dev-cumulus_ecs_cluster_profile"

tf-module/ds_img_to_ecr/variables.tf

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,38 @@ variable "ecr_repo_name" {
2323
description = "The name of the ECR repository"
2424
default = "unity-data-services"
2525
type = string
26+
}
27+
28+
variable "ami_id" {
29+
description = "The AMI ID for the EC2 instance"
30+
type = string
31+
}
32+
33+
variable "instance_type" {
34+
description = "The type of EC2 instance"
35+
default = "t3.medium"
36+
type = string
37+
}
38+
39+
variable "key_name" {
40+
description = "The name of the SSH key pair"
41+
type = string
42+
}
43+
44+
variable "ec2_profile_iam_name" {
45+
description = "EC2 Profile IAM name. Note that it is different from uds-sbx-cumulus_ecs_cluster_instance_role. uds-sbx-cumulus_ecs_cluster_profile is the correct name"
46+
type = string
47+
}
48+
49+
variable "security_group_id" {
50+
description = "Security Group ID which is setup for ssh connections, https connections, and so on.. "
51+
type = list(string)
52+
}
53+
variable "cumulus_lambda_vpc_id" {
54+
description = "VPC ID."
55+
type = string
56+
}
57+
variable "cumulus_subnet_id" {
58+
description = "subnet group ID where a new EC2 will be placed"
59+
type = string
2660
}

0 commit comments

Comments
 (0)