Skip to content

Instantly share code, notes, and snippets.

@estenssoros
Created September 20, 2023 07:35
Show Gist options
  • Save estenssoros/fe01a2a2a93d2cbacf8d67c7938f90b5 to your computer and use it in GitHub Desktop.
Save estenssoros/fe01a2a2a93d2cbacf8d67c7938f90b5 to your computer and use it in GitHub Desktop.
Docker, ECR, Elastic Beanstalk, & Terraform instance_profile.tf
resource "aws_iam_role" "ebs" {
name = "${local.common_name}-ebs"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "web_tier" {
role = aws_iam_role.ebs.name
policy_arn = "arn:aws:iam::aws:policy/AWSElasticBeanstalkWebTier"
}
resource "aws_iam_role_policy_attachment" "multicontainre_docker" {
role = aws_iam_role.ebs.name
policy_arn = "arn:aws:iam::aws:policy/AWSElasticBeanstalkMulticontainerDocker"
}
resource "aws_iam_role_policy_attachment" "worker_tier" {
role = aws_iam_role.ebs.name
policy_arn = "arn:aws:iam::aws:policy/AWSElasticBeanstalkWorkerTier"
}
## add other role bindings to your resources here
## ECR
# ---
resource "aws_iam_policy" "ecr" {
name = "${local.common_name}-ECRFullAccessPolicy"
description = "Provides full access to Amazon Elastic Container Registry (ECR)"
policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Action = [
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:GetRepositoryPolicy",
"ecr:DescribeRepositories",
"ecr:ListImages",
"ecr:BatchGetImage"
],
Effect = "Allow",
Resource = aws_ecr_repository.app.arn,
},
{
Action = [
"ecr:GetAuthorizationToken"
]
Effect = "Allow"
Resource = "*"
}
],
})
}
resource "aws_iam_role_policy_attachment" "ecr" {
role = aws_iam_role.ebs.name
policy_arn = aws_iam_policy.ecr.arn
}
resource "aws_iam_instance_profile" "beanstalk_iam_instance_profile" {
name = "${local.common_name}-beanstalk-iam-instance-profile"
role = aws_iam_role.ebs.name
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment