Skip to content

Instantly share code, notes, and snippets.

@jwkidd3
Created March 5, 2025 14:28
Show Gist options
  • Select an option

  • Save jwkidd3/bd924f58ef4cca4bbe08094bb24d9bc7 to your computer and use it in GitHub Desktop.

Select an option

Save jwkidd3/bd924f58ef4cca4bbe08094bb24d9bc7 to your computer and use it in GitHub Desktop.

Revisions

  1. jwkidd3 created this gist Mar 5, 2025.
    61 changes: 61 additions & 0 deletions iamrole.tf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    resource "aws_iam_role" "example" {
    name = "example-role"

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

    tags = {
    TagKey = "TagValue"
    }
    }

    resource "aws_iam_policy" "example" {
    name = "example-policy"
    description = "Example policy"

    policy = jsonencode({
    Version = "2012-10-17",
    Statement = [
    {
    Action = [
    "s3:GetObject",
    "s3:ListBucket",
    ],
    Effect = "Allow",
    Resource = [
    "arn:aws:s3:::example-bucket/*",
    "arn:aws:s3:::example-bucket",
    ],
    },
    ],
    })
    }

    resource "aws_iam_role_policy_attachment" "example" {
    role = aws_iam_role.example.name
    policy_arn = aws_iam_policy.example.arn
    }

    #Optional: Instance Profile, to use the role with EC2 instances.
    resource "aws_iam_instance_profile" "example" {
    name = "example-instance-profile"
    role = aws_iam_role.example.name
    }

    output "iam_role_arn" {
    value = aws_iam_role.example.arn
    }

    output "iam_instance_profile_arn" {
    value = aws_iam_instance_profile.example.arn
    }