Use IAM role instead of credentials to create aws resource from an EC2 instance using terraform


We are working on a requirement where we want terraform apply which runs on AWS EC2 instance to use IAM role instead of using credentials(accesskey/secretkey) as part of aws provider to create route53 in AWS.
NOTE: IAM Role added to instance has been provided with policy which gives the role the route53fullaccess.
When we use below syntax in, it works fine. We are able to create route.

But, we want the terraform script to run with IAM Role and not with credentials. (Do not want to maintain credentials file)
1. Removed provider block from file and run the build.
resource “aws_route53_record {}
2.Getting the below error. :InvalidClientTokenid.
3. Went through the terraform official documentation to use IAM Role. it says to use metadata api. but there is no working sample. (
Am new to Terraforms so pardon me if its a basic question. Can someone help with the code/working sample to achieve this ?


You need to supply the profile arn in the “provider” block, not the role, like so :

provider "aws" {
profile = "arn:aws:iam::<your account>:instance-profile/<your role name>"

The ‘role_arn’ key mentioned in the answer above is actually invalid in the ‘provider’ context.

Leave a Reply