Infra & Cloud terraform Beginner

AWS IAM Role for EC2

Terraform resource to create an IAM role with instance profile for EC2.

terraform
resource "aws_iam_role" "ec2" {
  name = "myapp-ec2-role"
  assume_role_policy = jsonencode({
    Version   = "2012-10-17"
    Statement = [{ Action = "sts:AssumeRole", Effect = "Allow", Principal = { Service = "ec2.amazonaws.com" } }]
  })
}
resource "aws_iam_instance_profile" "ec2" {
  name = "myapp-profile"
  role = aws_iam_role.ec2.name
}

Prerequisites

  • terraform >= 1.0
  • AWS provider

Pro Tips

  • → Attach policies with aws_iam_role_policy_attachment.

Was this module useful?

Share your experience — it helps the team improve.

Leave Feedback

Commercial Licensing

Acquire a production access token or template license for enterprise deployment.

Module Info

Language
terraform
Level
Beginner
Read time
3 min
Published
Dec 2024
License
Commercial License

Newsletter

New modules every week, directly to your inbox.