Skip to content

Instantly share code, notes, and snippets.

@surendrakonna
Created November 23, 2020 03:29
Show Gist options
  • Save surendrakonna/f11bac844ddcf70862451bb89eec0a32 to your computer and use it in GitHub Desktop.
Save surendrakonna/f11bac844ddcf70862451bb89eec0a32 to your computer and use it in GitHub Desktop.
AWS lambda function to delete a file from s3 bucket

Create lambda function to delete a file from s3

  1. Create IAM Role with s3 access
  2. Create Lambda function(delete-s3-file) with Ruby 2.7
require 'aws-sdk-s3'
S3_CLIENT = Aws::S3::Client.new

def lambda_handler(event:, context:)
   resp = S3_CLIENT.delete_object({
     bucket: event['bucket'], 
     key: event['file_path']
   })
   { statusCode: 200, body: resp.to_h }
end
  1. Change Retry attempts settings to 0 in Asynchronous invocation section since we don't need to retry in our usecase
  2. Attach s3 delete access role which is created in step 1

Create Group in IAM for ca-next

  1. Create group(ca-next-developers) with s3 access
  2. Add Lambda invoke access policy
  • Edit group -> Permission -> Inline Policies -> Create one
  • Choose Policy Generator and click on Select
  • Allow AWS lambda InvokeAsync and InvokeFunction
  1. Add our existing user to group
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment