AWS Builder Center
From admin to least privilege: locking down a Bedrock script with Terraform

From admin to least privilege: locking down a Bedrock script with Terraform

I built a small Amazon Bedrock script, then used Terraform to create a role that can call one model and nothing else. Here’s the test, the mistake that stopped me and what I took from it.

Cloud & DevOps | Terraform, IAM, CI/CD | Writing about what breaks
I wrote a small Python script that calls Amazon Bedrock, and I ran it as my admin user. It worked, but an admin can do anything in my AWS account, so if those credentials ever leaked, so would access to everything.
So I used Terraform to build a role that can call one model, Amazon Nova Micro, and nothing else. This is how I built it, how I tested it, and the mistakes along the way.

What I built

I created a least-privilege IAM role with Terraform. A role is an identity that holds a set of permissions, and a user can step into it. Mine can call one model and nothing else.
There are three parts:
  • The policy gives permission to call one model, and it is limited to that model's ARN.
  • The role has the policy attached to it.
  • The trust policy says who is allowed to use the role. Only my  grace-admin  user can step into it.
 terraform plan  said 3 to add, and it worked.

The Terraform code

Here's the core of it. The policy allows one action on one model:
data "aws_iam_policy_document" "invoke_nova_micro" {
statement {
actions = ["bedrock:InvokeModel"]
resources = ["arn:aws:bedrock:us-east-1::foundation-model/amazon.nova-micro-v1:0"]
}
}
The trust policy decides who can use the role. I limited it to my user:
data "aws_iam_policy_document" "trust" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "AWS"
identifiers = ["arn:aws:iam::<account-id>:user/grace-admin"]
}
}
}
In my file the account ID is looked up automatically, so it isn't written into the code. If you use a different model, change the model ARN to match.

The test

I ran my Bedrock script through the role, and it worked, because the role can call Nova Micro.
Then I ran  aws s3 ls  through the same role, and it failed with AccessDenied, because the role can't touch S3. That failure was necessary. It showed least privilege working.

The same role: the Bedrock call worked, and the S3 call was denied.

How to try it yourself

  1. Run  terraform init ,  terraform plan  and  terraform apply . It prints the role's ARN.
  1. Add a profile to  ~/.aws/config  that assumes the role:
[profile bedrock-role]
role_arn = arn:aws:iam::<account-id>:role/bedrock-nova-micro-role
source_profile = default
region = us-east-1
  1. Run your Bedrock script through the role:
AWS_PROFILE=bedrock-role python hello.py
  1. Check that it can't do anything else:
aws s3 ls --profile bedrock-role
The first should work and the second should be denied.

The mistake that stopped everything

When I added the role ARN to my AWS config file in vim, I copied it exactly as Terraform printed it, with the quotation marks. The AWS CLI then said the ARN was invalid.
At first I didn't see why. I went back and corrected the file several times before I noticed the quotes. Terraform prints values in quotes, and the CLI reads them as part of the ARN. Once I removed them, the script ran.

Errors you might hit

  • "ARN is invalid": check for quotation marks or extra characters in the  role_arn  line of your config file.
  • A  ParamValidationError  about  modelId : the parameter name is  modelId . I had typed  MODEL_ID  as the name.
  •  AccessDenied  on the Bedrock call: the role's policy doesn't match the model you're calling, so check the model ARN.
  • "Not authorized to perform sts:AssumeRole": the trust policy doesn't list your user, or the user name has a typo.

Tips

  • Run  git status  before every commit. Mine showed  terraform.tfstate  about to be committed, and I removed it and added a  .gitignore . State files can contain sensitive data.
  • Replace your account ID with a placeholder before sharing any code or screenshots.
  • Roles and policies cost nothing, but run  terraform destroy  when you're done with a practice project.

What I learned

Making mistakes is part of learning. The ARN quotes are a good example, and I'm fairly sure it won't happen to me again.
I'm refreshing my DevOps journey. I first learned much of this a year ago, and since then life and my career have changed, but that's a story for another day.

What's next

Next I'll containerize the summarizer with Docker.
The code is in  terraform-bedrock-iam , with a GitHub Actions workflow that checks it.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article