Grant AWS Lambda Access to S3 Bucket or CloudWatch Logs

avatar
Borislav Hadzhiev

Last updated: Feb 26, 2024
5 min

banner

# Table of Contents

  1. Grant AWS Lambda Access to an S3 Bucket
  2. Grant a Lambda Function access to CloudWatch Logs

# Grant AWS Lambda Access to an S3 Bucket

To grant a Lambda function access to an S3 Bucket, we have to attach an IAM policy to the function's execution role.

The policy should grant permissions for all the Actions the function needs to perform on the specified bucket.

For example, the following policy grants permission to upload objects to a specific S3 bucket.

The policy applies to a specific bucket, so make sure to replace the YOUR_BUCKET placeholder with your bucket's name.
upload-object-to-s3.json
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:PutObject", "s3:PutObjectAcl", "s3:GetObject", "s3:GetObjectAcl", "s3:AbortMultipartUpload" ], "Resource": [ "arn:aws:s3:::YOUR_BUCKET", "arn:aws:s3:::YOUR_BUCKET/*" ] } ] }

The permissions you have to grant your lambda function are use-case dependent.

You could set "s3:*" for the Action element in the policy to grant full S3 access to the lambda function. However, it's best practice to grant an entity the least permissions that get the job done.

You can view a full list of the S3 Actions by visiting the docs.

There is a Description column that explains what each action does.

Most of the action names are intuitive, e.g. DeleteObject, GetObject, PutObject, etc.

To attach a policy to the lambda function's execution role:

  1. Open the AWS Lambda console and click on your function's name.
  2. Click on the Configuration tab and then click Permissions.

click on function role

  1. Click on the function's role.

  2. Click on Add Permissions, then Attach policies and click the Create policy button.

  3. In the JSON editor paste the following policy.

Replace the YOUR_BUCKET placeholder and adjust the Actions your lambda function needs to execute.
example-s3-policy.json
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:Put*", "s3:Get*", "s3:Delete*" ], "Resource": [ "arn:aws:s3:::YOUR_BUCKET/*" ] } ] }
  1. Click Next: Tags, then Next: Review and give your policy a name, then click Create policy.

  2. In the browser tab with the function's role, refresh the page to load the new policy and filter by the policy's name. filter by policy name

  3. Click on the checkbox next to the policy and click Attach Policies.

At this point, the lambda function's role has been extended with a policy that grants access to some S3 actions on a specific bucket.

It can take up to a minute until the IAM changes have been propagated and the policy is in effect.

Invoke your lambda function and verify whether it has access to the S3 bucket.

If your function is still unable to access S3, try to increase the function's timeout by a second in the AWS console, or simply add an extra print statement in the code and click the Deploy button.

If your Lambda function still doesn't have access to the S3 bucket, expand the IAM policy you added to the function's role and edit it to look like the policy below.

edit policy

Replace the YOUR_BUCKET placeholder with the name of your S3 bucket.
s3-full-access.json
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:*" ], "Resource": [ "arn:aws:s3:::YOUR_BUCKET", "arn:aws:s3:::YOUR_BUCKET/*" ] } ] }

The IAM policy above grants full access to an S3 bucket, so your Lambda function will be able to execute all S3 actions on the bucket.

It's best practice to grant the least possible permissions that enable you to get the job done, however, the asterisk * symbol is useful when debugging.

After you've updated the policy, try to invoke your Lambda function again. It should have permission to execute any action on the S3 bucket.

You can make the IAM policy less permissive after you verify which actions your lambda needs to run.

Note that a policy statement with a Deny effect will always override any Allow statements.

I've also written a tutorial on how to add permissions to Lambda functions in AWS CDK.

# Grant a Lambda Function access to CloudWatch Logs

To grant a Lambda function permissions to log to CloudWatch, we have to attach the AWSLambdaBasicExecutionRole AWS-managed policy to the function's execution role.

The IAM policy grants permissions for the logs:CreateLogGroup, logs:CreateLogStream and logs:PutLogEvents actions.

To attach the IAM policy to your Lambda function's role:

  1. Open the AWS Lambda console and click on your function's name.
  2. Click on the Configuration tab and click on Permissions in the sidebar.

click on role

  1. Click on the role's name.
  2. In the Permissions tab of the role, click on Add permissions and Attach policies.
  3. Filter for AWSLambdaBasicExecutionRole in the search input and click the checkbox next to the first result.

attach cloudwatch logs policy

The AWSLambdaBasicExecutionRole managed policy contains the following statement that allows our function to log to CloudWatch:

lambda-cloudwatch-logs.json
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents" ], "Resource": "*" } ] }
  1. Click on the Attach policies button
  1. Invoke your lambda function to generate some logs.

  2. In the browser tab of the lambda function, click on the Monitor tab and select View logs in CloudWatch.

view logs in cloudwatch

  1. Click on the most recent log stream and you should see the logs of your Lambda function.
If you still are unable to see any logs being produced, make a small change to the lambda function, e.g. increase its timeout by 1 second or add an extra print statement in the function's code and click on the Deploy button.

To get a better view of the logs, click on the View as text checkbox at the top. It displays the logs as text, rather than a series of collapsible rows:

view logs as text

Note that AWS Lambda automatically creates a log group with the name /aws/lambda/your-lambda-name as long as the function has the necessary permissions.

I've also written a tutorial on how to add permissions to Lambda functions in AWS CDK.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.