Skip to content

Commit

Permalink
Adding IAM instance profile
Browse files Browse the repository at this point in the history
  • Loading branch information
JDeBo committed Dec 22, 2023
1 parent 1276111 commit 43e30bb
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
77 changes: 77 additions & 0 deletions linux/terraform/iam_profile.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
data "aws_caller_identity" "current" {}

resource "aws_iam_role" "this" {
name = "ec2-s3-lambda"

assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Action = "sts:AssumeRole",
Effect = "Allow",
Principal = {
Service = "ec2.amazonaws.com"
}
}
]
})
}

resource "aws_iam_policy" "s3_lambda_admin" {
name = "s3_lambda_admin_policy"
description = "Admin access to S3 and Lambda"

policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Action = [
"s3:*",
"lambda:*"
],
Effect = "Allow",
Resource = "*"
}
]
})
}

resource "aws_iam_policy" "ec2_read" {
name = "ec2_read_policy"
description = "Read access to EC2"

policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Action = [
"ec2:Describe*"
],
Effect = "Allow",
Resource = "*"
},
{
Action = [
"iam:*Role*"
],
Effect = "Allow",
Resource = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/lambda*"
}
]
})
}

resource "aws_iam_role_policy_attachment" "s3_lambda_admin_attach" {
role = aws_iam_role.this.name
policy_arn = aws_iam_policy.s3_lambda_admin.arn
}

resource "aws_iam_role_policy_attachment" "ec2_read_attach" {
role = aws_iam_role.this.name
policy_arn = aws_iam_policy.ec2_read.arn
}

resource "aws_iam_instance_profile" "this" {
name = "ec2-lambda-s3"
role = aws_iam_role.this.name
}
1 change: 1 addition & 0 deletions linux/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ module "team_servers" {
EOF

tags = local.tags
iam_instance_profile = aws_iam_instance_profile.this.id
}

resource "aws_key_pair" "main" {
Expand Down

0 comments on commit 43e30bb

Please sign in to comment.