CodexBloom - Programming Q&A Platform

Terraform Not Creating AWS IAM Role with Inline Policies for Lambda Function

👀 Views: 11 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-15
terraform aws iam HCL

I'm relatively new to this, so bear with me. I'm trying to set up an AWS Lambda function using Terraform, and I need to create an IAM role that has inline policies attached to it. However, when I apply my configuration, the role is created, but the inline policies are not being attached as expected. I am using Terraform version 1.3.7 and the AWS provider version 4.3.0. Here's the relevant part of my `main.tf`: ```hcl resource "aws_iam_role" "lambda_role" { name = "my_lambda_role" assume_role_policy = jsonencode({ Version = "2012-10-17" Statement = [ { Action = "sts:AssumeRole" Principal = { Service = "lambda.amazonaws.com" } Effect = "Allow" Sid = "" } ] }) } resource "aws_iam_policy" "lambda_policy" { name = "my_lambda_policy" description = "My policy for Lambda" policy = jsonencode({ Version = "2012-10-17" Statement = [ { Action = ["s3:ListBucket"], Effect = "Allow", Resource = "*" } ] }) } resource "aws_iam_role_policy_attachment" "lambda_policy_attachment" { policy_arn = aws_iam_policy.lambda_policy.arn role = aws_iam_role.lambda_role.name } ``` I've verified that the IAM policy is being created correctly, and I see it in the AWS console. However, the inline policies are not appearing under the role in the console. I tried running `terraform plan` and `terraform apply`, and they both show no errors, but the expected behavior isn't happening. I also attempted to attach the policy directly using an inline policy block instead, like this: ```hcl resource "aws_iam_role" "lambda_role" { name = "my_lambda_role" assume_role_policy = jsonencode({ Version = "2012-10-17" Statement = [ { Action = "sts:AssumeRole" Principal = { Service = "lambda.amazonaws.com" } Effect = "Allow" Sid = "" } ] }) inline_policy { name = "my_lambda_inline_policy" policy = jsonencode({ Version = "2012-10-17" Statement = [ { Action = ["s3:ListBucket"], Effect = "Allow", Resource = "*" } ] }) } } ``` However, this didn't work either, and I still don't see the policy attached. Is there something I'm missing in the configuration or any best practices I should be following? Any help would be appreciated! This is for a web app running on Ubuntu 22.04.