Skip to content

Instantly share code, notes, and snippets.

@jwkidd3
Created March 5, 2025 14:28
Show Gist options
  • Save jwkidd3/bd924f58ef4cca4bbe08094bb24d9bc7 to your computer and use it in GitHub Desktop.
Save jwkidd3/bd924f58ef4cca4bbe08094bb24d9bc7 to your computer and use it in GitHub Desktop.
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment