Note: Scroll down this article to download the terrafom files in zip format.


Terraform AWS IAM Role for EC2

This Terraform module provisions an AWS IAM Role and an associated Instance Profile. The role is configured with a custom IAM policy that grants specific permissions for EC2 and SNS services. The role is designed to be assumed by the EC2 service, making it suitable for attachment to an EC2 instance.


Resources Created

This configuration creates the following AWS resources:

  • aws_iam_policy.custom_permissions_policy
  • aws_iam_role.custom_role
  • aws_iam_role_policy_attachment.custom_attachment
  • aws_iam_instance_profile.instance_profile

Prerequisites

  • Terraform installed on your local machine.
  • AWS Credentials configured for your environment (e.g., via AWS CLI, environment variables).

Deployment

  1. Initialize Terraform:
    terraform init
    
  2. (Optional) Plan the deployment:
    terraform plan
    
  3. Apply the configuration:
    terraform apply
    

You can customize the resource names and AWS region by modifying the terraform.auto.tfvars file.


Inputs

The following input variables are defined in variables.tf:

NameDescriptionTypeDefault Value
aws_regionThe AWS region where resources will be created.string"us-east-1"
policy_nameThe name for the custom IAM policy.string"csc-EC2-Permissions-Policy"
role_nameThe name for the custom IAM role.string"csc-EC2-Instance-Role"

Outputs

The following outputs will be displayed after the configuration is applied:

NameDescription
iam_role_nameThe name of the created IAM role.
instance_profile_nameThe name of the created IAM instance profile.