A developer is configuring a custom IAM role named `ApplicationLogWriterRole` for a new AWS Lambda function that must write logs to an Amazon S3 bucket. The developer attempts to define both the trust relationship and the S3 permissions in a single policy document when creating the role. The developer applies the following JSON document as the role's trust policy (Assume Role Policy):
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": [
"sts:AssumeRole",
"s3:PutObject"
],
"Resource": "arn:aws:s3:::app-logs-2026/*"
}
]
}
No other policies are attached to the IAM role. When the Lambda function executes and attempts to upload a log file to the S3 bucket, it receives an `AccessDenied` error. How should the developer resolve this issue?
- AUpdate the trust policy's Resource element to "*" and remove the "sts:AssumeRole" action since the role is already associated with the Lambda function.
- Modify the trust policy to only allow the "sts:AssumeRole" action for the Lambda service principal, and attach a separate IAM identity-based policy to the role that grants the "s3:PutObject" permission on the S3 bucket.Answer
- CAdd a resource-based policy to the S3 bucket that permits "sts:AssumeRole" for the Lambda service principal, and remove the trust policy from the IAM role.
- DEmbed the AWS access key ID and secret access key for an IAM user with S3 write permissions directly in the Lambda function's code to bypass the IAM role.