{"id":1247,"date":"2023-12-27T19:23:38","date_gmt":"2023-12-27T19:23:38","guid":{"rendered":"http:\/\/obl.icz.temporary.site\/?p=1247"},"modified":"2024-01-07T23:52:03","modified_gmt":"2024-01-07T23:52:03","slug":"lambda-with-s3-trigger-using-aws-sdk-and-python","status":"publish","type":"post","link":"https:\/\/www.maghilda.com\/staging\/9669\/lambda-with-s3-trigger-using-aws-sdk-and-python\/","title":{"rendered":"HOWTO: Lambda with S3 trigger using AWS SDK and python"},"content":{"rendered":"\n<p>In this tutorial, you will use the AWS SDK and Python to create a Lambda function and configure a trigger for an Amazon Simple Storage Service (Amazon S3) bucket. Every time you add an object to your Amazon S3 bucket, your lambda function runs and outputs the object type to Amazon CloudWatch Logs.<\/p>\n\n\n\n<p>You can use a Lambda function with an Amazon S3 trigger to perform many types of file processing tasks. For example, you can use a Lambda function to create a thumbnail whenever an image file is uploaded to your Amazon S3 bucket, or to convert uploaded documents into different formats.&nbsp;<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">AWS Services Used:<\/h5>\n\n\n\n<ul class=\"wp-block-list\" style=\"padding-right:var(--wp--preset--spacing--50);padding-left:var(--wp--preset--spacing--50)\">\n<li>Amazon S3<\/li>\n\n\n\n<li>AWS IAM<\/li>\n\n\n\n<li>AWS Lambda<\/li>\n\n\n\n<li>AWS CloudWatch Logs<\/li>\n<\/ul>\n\n\n\n<p style=\"margin-top:0;margin-right:0;margin-bottom:0;margin-left:0;padding-top:0;padding-right:0;padding-bottom:0;padding-left:0\">To complete this tutorial, you perform the following steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\" style=\"padding-right:var(--wp--preset--spacing--50);padding-left:var(--wp--preset--spacing--50)\">\n<li>Review IAM user permissions<\/li>\n\n\n\n<li>Upload an object to your S3 bucket<\/li>\n\n\n\n<li>Create IAM permissions policy<\/li>\n\n\n\n<li>Create IAM execution role for your lambda function<\/li>\n\n\n\n<li>Create a Lambda function that returns the object type of objects in an Amazon S3 bucket.<\/li>\n\n\n\n<li>Deploy the Lambda function<\/li>\n\n\n\n<li>Configure a Lambda trigger that invokes your function when objects are uploaded to your bucket.<\/li>\n\n\n\n<li>Test your function using the trigger.<\/li>\n\n\n\n<li>Clean up resources <\/li>\n<\/ol>\n\n\n\n<h5 class=\"wp-block-heading\">Prerequisites<\/h5>\n\n\n\n<p>Follow the instructions in the <a href=\"http:\/\/obl.icz.temporary.site\/easy-steps-to-aws-with-aws-cli-sdk\/\">previous tutorial<\/a> to set up users and create bucket. After you have created a bucket, follow the steps below:<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Assign permissions to your IAM user<\/strong><\/h5>\n\n\n\n<p style=\"margin-top:0;margin-right:0;margin-bottom:0;margin-left:0;padding-top:0;padding-right:0;padding-bottom:0;padding-left:0\">Your IAM user requires the following policies:<\/p>\n\n\n\n<ul class=\"wp-block-list\" style=\"padding-right:var(--wp--preset--spacing--50);padding-left:var(--wp--preset--spacing--50)\">\n<li>AmazonS3FullAccess \u2013 Grants permissions to all Amazon S3 actions, including permissions to create and use an Object Lambda Access Point.&nbsp;<\/li>\n\n\n\n<li>AWSLambda_FullAccess \u2013 Grants permissions to all Lambda actions.&nbsp;<\/li>\n\n\n\n<li>IAMFullAccess \u2013 Grants permissions to all IAM actions.&nbsp;<\/li>\n\n\n\n<li>IAMAccessAnalyzerReadOnlyAccess \u2013 Grants permissions to read all access information provided by IAM Access Analyzer.&nbsp;<\/li>\n\n\n\n<li>CloudWatchLogsFullAccess \u2013 Grants full access to CloudWatch Logs.<\/li>\n<\/ul>\n\n\n\n<p>In the previous tutorial, you provided Full Administrative Access to ALL AWS services, hence you don\u2019t need the above IAM policies for this tutorial but the above is a good security practice and ought to replace the Full Administrative Access in production workloads.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Upload an image file to your bucket \u2018vibstest\u2019<\/strong><\/h5>\n\n\n\n<p>For this tutorial to work, in your s3 bucket, uncheck <strong>Block Public access<\/strong>  under permissions tab<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import boto3\nfrom botocore.exceptions import ClientError\nimport logging\nimport os\n\ndef upload_file(file_name, bucket, object_name=None):\n\n    print('***************upload_file *****************')\n    # If S3 object_name was not specified, use file_name\n    if object_name is None:\n        object_name = os.path.basename(file_name)\n\n    # Upload the file\n    s3_client = boto3.client('s3')\n    try:\n        response = s3_client.upload_file(file_name, bucket, object_name)\n    except ClientError as e:\n        logging.error(e)\n        return False\n    return True\n\n#upload a file to the bucket\nresponse = upload_file(file_name,bucket)\nprint(response)\n\n#call the function\nupload_file('vacation.jpg', 'vibstest')<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Create IAM permissions policy<\/strong><\/h5>\n\n\n\n<p>Before you can create an execution role for you Lambda function, you first create a permissions policy to give your function permission to access the required AWS resources. For this tutorial, the policy allows Lambda to get objects from an Amazon S3 bucket and to write to Amazon CloudWatch Logs.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import boto3\nfrom botocore.exceptions import ClientError\nimport logging\nimport os\nimport json\n\ndef create_iam_policy(policy_name):\n\n        print('***************create_iam_policy *****************')\n        my_iam_policy = {\n        \"Version\": \"2012-10-17\",\n        \"Statement\": &#91;\n            {\n                \"Effect\": \"Allow\",\n                \"Action\": &#91;\n                    \"logs:PutLogEvents\",\n                    \"logs:CreateLogGroup\",\n                    \"logs:CreateLogStream\"\n                ],\n                \"Resource\": \"arn:aws:logs:*:*:*\"\n            },\n            {\n                \"Effect\": \"Allow\",\n                \"Action\": &#91;\"s3:GetObject\"],\n                \"Resource\": \"arn:aws:s3:::*\/*\"\n            }\n            ]\n        }\n        \n        # Convert the policy from JSON dict to string\n        my_iam_policy = json.dumps(my_iam_policy)\n        print(my_iam_policy)\n\n        iam = boto3.client('iam')\n\n        # Set the new policy\n        try:\n            response = iam.create_policy(PolicyName=policy_name, PolicyDocument=my_iam_policy)\n            print(response)\n        except ClientError as e:\n             logging.error(e)\n             return False\n        return True\n\n#call the function\nprint(create_iam_policy('s3-trigger-vibs'))<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Create IAM execution role<\/strong><\/h5>\n\n\n\n<p>An execution role is an AWS Identity and Access Management (IAM) role that grants a Lambda function permission to access AWS services and resources. To enable your function to get objects from an Amazon S3 bucket, you attach the permissions policy you created in the previous step.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import boto3\nfrom botocore.exceptions import ClientError\nimport logging\nimport os\nimport json\n\ndef create_lambda_execution_role(rolename):\n         iam = boto3.client('iam')\n\n         print('***************create_lambda_execution_role *****************')\n         trust_policy = {\n                \"Version\": \"2012-10-17\",\n                \"Statement\": &#91;\n                    {\n                        \"Effect\": \"Allow\",\n                        \"Action\": &#91;\"sts:AssumeRole\"],\n                        \"Principal\": {\"Service\": \"lambda.amazonaws.com\"}\n                    }\n                ]\n            }\n         \n         # Convert the policy from JSON dict to string\n         trust_policy = json.dumps(trust_policy)\n\n         try:\n            #create role\n            response = iam.create_role(RoleName=rolename,\n                                        AssumeRolePolicyDocument=trust_policy)\n            print(response)\n            \n            #attach the iam policy to this newly created iam role.\n            response = iam.attach_role_policy(\n                    PolicyArn='arn:aws:iam::111111222222:policy\/s3-trigger-vibs',\n                    RoleName=rolename,\n                )\n            print(response)\n         except ClientError as e:\n             logging.error(e)\n             return False\n         return True\n\n#call the function\nprint(create_lambda_execution_role('lambda-s3-trigger-role'))<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Create the function code<\/strong><\/h5>\n\n\n\n<p>Your Lambda function will retrieve the key name of the uploaded object and the name of the bucket from the event parameter it receives from Amazon S3.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import urllib.parse\nimport boto3\nfrom botocore.exceptions import ClientError\nimport logging\nimport os\nimport json\n\nprint('Loading function')\n\ns3 = boto3.client('s3')\n\ndef lambda_handler(event, context):\n    #print(\"Received event: \" + json.dumps(event, indent=2))\n\n    # Get the object from the event and show its content type\n    bucket = event&#91;'Records']&#91;0]&#91;'s3']&#91;'bucket']&#91;'name']\n    key = urllib.parse.unquote_plus(event&#91;'Records']&#91;0]&#91;'s3']&#91;'object']&#91;'key'], encoding='utf-8')\n    try:\n        response = s3.get_object(Bucket=bucket, Key=key)\n        print(\"CONTENT TYPE: \" + response&#91;'ContentType'])\n        return response&#91;'ContentType']\n    except Exception as e:\n        print(e)\n        print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))\n        raise e<\/code><\/pre>\n\n\n\n<p>Save the above in a new python file and name it test.py<br>Zip the file and upload to s3<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>zip function.zip test.py  #i named the zip file function.zip<\/code><\/pre>\n\n\n\n<p>Upload the function.zip file to your bucket<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#call the function\nupload_file('function.zip', 'vibstest')<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Deploy the Lambda function<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>import boto3\nfrom botocore.exceptions import ClientError\nimport logging\n\ndef create_lambda_function(functionName):\n     \n    lam = boto3.client(\u2018lambda')\n    print('***************create_lambda_function *****************')\n\n    try:\n        response =  lam.create_function(\n            FunctionName=functionName,\n            Runtime='python3.11',\n            Handler=\u2018test.lambda_handler\u2019, #test is the name of the file with lambda_handler function\n            Code={'S3Bucket': 'vibstest', 'S3Key': 'function.zip'},\n            Role='arn:aws:iam::111111222222:role\/lambda-s3-trigger-role'\n        )\n        print(response)\n    except ClientError as e:\n        logging.error(e)\n        return False\n    return True\n\n#call the function\nprint(create_lambda_function('vibsfunction'))<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Create the Amazon S3 trigger<\/strong><\/h5>\n\n\n\n<p>Now that you have deployed your function code above, you create the Amazon S3 trigger that will invoke your function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import boto3\nfrom botocore.exceptions import ClientError\nimport logging\n\n\ndef create_trigger_s3(bucketName):\n    s3 = boto3.client('s3')\n\n    print('***************create_trigger_s3 *****************')\n    #first add permissions on the lambda to allow S3 to invoke the lambda function\n    lam = boto3.client('lambda')\n    response = lam.add_permission(\n        FunctionName='vibsfunction',\n        StatementId='1',\n        Action='lambda:InvokeFunction',\n        Principal='s3.amazonaws.com',\n        SourceArn='arn:aws:s3:::vibstest',\n        SourceAccount='111111222222'\n    )\n    # create s3 notification for lambda function\n    try:\n        response = s3.put_bucket_notification_configuration(\n            Bucket=bucketName,\n            NotificationConfiguration= {\n                        'LambdaFunctionConfigurations':\n                            &#91;{'LambdaFunctionArn': 'arn:aws:lambda:us-east-1:111111222222:function:vibsfunction', \n                            'Events': &#91;'s3:ObjectCreated:*']}]\n                    }\n                    )\n        print(response)\n    except ClientError as e:\n        logging.error(e)\n        return False\n    return True\n\n#call the function\nprint(create_trigger_s3('vibstest'))<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Test the Lambda function with the Amazon S3 trigger<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>def check_logs():\n\n    print('***************check_logs function*****************')\n    client = boto3.client('logs')\n\n    ## For the latest\n    response = client.describe_log_streams(\n        logGroupName=\"\/aws\/lambda\/vibsfunction\", \n        orderBy='LastEventTime'\n        )\n\n    for log_stream in response&#91;'logStreams']:  \n        latestlogStreamName = log_stream&#91;'logStreamName']\n        print('logstream=========',latestlogStreamName)\n\n        response = client.get_log_events(\n             logGroupName='\/aws\/lambda\/vibsfunction',\n             logStreamName=latestlogStreamName\n        )\n        #print(response)\n\n        for event in response&#91;'events']:\n            #print(event)\n            print(event&#91;'message'])<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>def test_lambda():\n\n    print('***************test_lambda *****************')\n    #upload a file to the bucket\n    response = upload_file('vacation.jpg','vibstest')\n    print(response)\n\n    #check logs to see if the function ran successfully and the content type of the image uploaded\n    check_logs()<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>#call the function\ntest_lambda()<\/code><\/pre>\n\n\n\n<p><strong>Verify correct operation using CloudWatch Logs<\/strong><br>Running the test_lambda() function should show similar results. Please note the &#8216;Loading function&#8217; and CONTENT TYPE shows that the lambda function executed successfully.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>logstream========= 2023\/12\/27\/&#91;$LATEST]8800b75ff2594696aad710da757a5f62\nINIT_START Runtime Version: python:3.11.v25     Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:a3304f2b48f740276b97ad9c52a9cc36a0bd9b44fecf74d0f1416aafb74e92fc\n\n<strong>Loading function<\/strong>\n\nSTART RequestId: 8549ec7a-f994-4cd7-9be6-ba7968edd8fc Version: $LATEST\n\n<strong>CONTENT TYPE: binary\/octet-stream<\/strong>\n\nEND RequestId: 8549ec7a-f994-4cd7-9be6-ba7968edd8fc\n\nREPORT RequestId: 8549ec7a-f994-4cd7-9be6-ba7968edd8fc  Duration: 289.86 ms     Billed Duration: 290 ms Memory Size: 128 MB     Max Memory Used: 83 MB  Init Duration: 483.76 ms   <\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Clean up your resources<\/strong><\/h5>\n\n\n\n<ol class=\"wp-block-list\" style=\"padding-right:var(--wp--preset--spacing--50);padding-left:var(--wp--preset--spacing--50)\">\n<li><strong>Delete the Lambda function<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>def delete_lambda_function(functionName):\n     \n     print('***************delete_lambda_function function*****************')\n     lam = boto3.client('lambda')\n\n     try:\n        response = lam.delete_function(\n            FunctionName=functionName\n        )\n        print(response)\n        print('function deleted')\n     except ClientError as e:\n        logging.error(e)\n        return False\n     return True<\/code><\/pre>\n\n\n\n<p>2. <strong>Delete the IAM policy<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def delete_iam_policy(rolename, policyarn):\n\n    print('***************delete_iam_policy *****************')\n    iam = boto3.client('iam')\n\n    #first have to detach the policy\n    response = iam.detach_role_policy(\n                RoleName=rolename,\n                PolicyArn=policyarn\n            )\n    print(response)\n\n    #delete the policy\n    response = iam.delete_policy(\n                PolicyArn=policyarn\n            )\n    print(response)<\/code><\/pre>\n\n\n\n<p>3. <strong>Delete the lambda execution role<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def delete_lambda_execution_role(rolename):\n\n    print('***************delete_lambda_execution_role *****************')\n    iam = boto3.client('iam')\n    response = iam.delete_role(RoleName=rolename)\n    print(response)<\/code><\/pre>\n\n\n\n<p>4. <strong>Delete the S3 bucket and all of its contents<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def delete_bucket(bucket_name):\n\n    print('***************delete_bucket *****************')\n    s3 = boto3.client('s3')\n\n    #list all the contents in the bucket\n    response = s3.list_objects_v2(Bucket=bucket_name)\n\n    #delete each object\n    for object in response&#91;'Contents']:\n        print('Deleting', object&#91;'Key'])\n        s3.delete_object(Bucket=bucket_name, Key=object&#91;'Key'])\n\n    try:\n        s3.delete_bucket(Bucket=bucket_name)\n    except ClientError as e:\n        logging.error(e)\n        return False\n    return True<\/code><\/pre>\n\n\n\n<p>5. <strong>Run the above functions<\/strong><br>Replace the variable names as needed<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def clean_up():\n\n    print('***************clean_up function*****************')\n    \n    bucketname='vibstest'\n    lambdafunctionname='vibsfunction'\n    policyarn='arn:aws:iam::111111222222:policy\/s3-trigger-vibs'\n    rolename='lambda-s3-trigger-role'\n\n    #delete lambda function 'vibsfunction'\n    print(delete_lambda_function(lambdafunctionname))\n\n    #detach and delete iam policy 's3-trigger-vibs'\n    delete_iam_policy(rolename,policyarn)\n\n    #delete lambda execution role 'lambda-s3-trigger-role'\n    delete_lambda_execution_role(rolename)\n    \n    #delete bucket and its contents 'vibstest'\n    delete_bucket(bucketname)\n\n#call the function\nclean_up()<\/code><\/pre>\n\n\n\n<p><strong>Note: You may also delete the test user.&nbsp;<\/strong><\/p>\n\n\n\n<h5 class=\"wp-block-heading\">References:<\/h5>\n\n\n\n<ul class=\"wp-block-list\" style=\"padding-right:var(--wp--preset--spacing--50);padding-left:var(--wp--preset--spacing--50)\">\n<li><a href=\"https:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/with-s3-example.html\">https:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/with-s3-example.html<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/aws.amazon.com\/tutorials\/run-serverless-code\/\">https:\/\/aws.amazon.com\/tutorials\/run-serverless-code\/<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/docs.aws.amazon.com\/AmazonS3\/latest\/userguide\/tutorial-s3-object-lambda-uppercase.html\">https:\/\/docs.aws.amazon.com\/AmazonS3\/latest\/userguide\/tutorial-s3-object-lambda-uppercase.html<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/docs.aws.amazon.com\/IAM\/latest\/UserGuide\/id_users_create.html#id_users_create_console\">https:\/\/docs.aws.amazon.com\/IAM\/latest\/UserGuide\/id_users_create.html#id_users_create_console<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/docs.aws.amazon.com\/AmazonCloudWatch\/latest\/logs\/iam-identity-based-access-control-cwl.html\">https:\/\/docs.aws.amazon.com\/AmazonCloudWatch\/latest\/logs\/iam-identity-based-access-control-cwl.html<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/boto3.amazonaws.com\/v1\/documentation\/api\/latest\/guide\/iam-example-policies.html\">https:\/\/boto3.amazonaws.com\/v1\/documentation\/api\/latest\/guide\/iam-example-policies.html<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/lambda-intro-execution-role.html\">https:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/lambda-intro-execution-role.html<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/boto3.amazonaws.com\/v1\/documentation\/api\/latest\/reference\/services\/iam\/client\/create_role.html\">https:\/\/boto3.amazonaws.com\/v1\/documentation\/api\/latest\/reference\/services\/iam\/client\/create_role.html<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/gettingstarted-awscli.html\">https:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/gettingstarted-awscli.html<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/lambda-python.html\">https:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/lambda-python.html<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will use the AWS SDK and Python to create a Lambda function and configure a trigger for an Amazon Simple Storage Service (Amazon S3) bucket. Every time you add an object to your Amazon S3 bucket, your lambda function runs and outputs the object type to Amazon CloudWatch Logs. You can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"disabled","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[12,24,25],"tags":[28,26,29,27],"class_list":["post-1247","post","type-post","status-publish","format-standard","hentry","category-aws-tutorial","category-lambda","category-s3","tag-aws-sdk","tag-lambda","tag-python","tag-s3"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>HOWTO: Lambda with S3 trigger using AWS SDK and python - maghilda<\/title>\n<meta name=\"description\" content=\"Invoke Lambda function with S3 trigger using AWS SDK and Python\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.maghilda.com\/staging\/9669\/lambda-with-s3-trigger-using-aws-sdk-and-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"HOWTO: Lambda with S3 trigger using AWS SDK and python - maghilda\" \/>\n<meta property=\"og:description\" content=\"Invoke Lambda function with S3 trigger using AWS SDK and Python\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.maghilda.com\/staging\/9669\/lambda-with-s3-trigger-using-aws-sdk-and-python\/\" \/>\n<meta property=\"og:site_name\" content=\"maghilda\" \/>\n<meta property=\"article:published_time\" content=\"2023-12-27T19:23:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-07T23:52:03+00:00\" \/>\n<meta name=\"author\" content=\"vibs\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"vibs\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.maghilda.com\\\/staging\\\/9669\\\/lambda-with-s3-trigger-using-aws-sdk-and-python\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.maghilda.com\\\/staging\\\/9669\\\/lambda-with-s3-trigger-using-aws-sdk-and-python\\\/\"},\"author\":{\"name\":\"vibs\",\"@id\":\"https:\\\/\\\/www.maghilda.com\\\/staging\\\/9669\\\/#\\\/schema\\\/person\\\/21009c5e4f1817ea18c81d5004bcec1e\"},\"headline\":\"HOWTO: Lambda with S3 trigger using AWS SDK and python\",\"datePublished\":\"2023-12-27T19:23:38+00:00\",\"dateModified\":\"2024-01-07T23:52:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.maghilda.com\\\/staging\\\/9669\\\/lambda-with-s3-trigger-using-aws-sdk-and-python\\\/\"},\"wordCount\":754,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.maghilda.com\\\/staging\\\/9669\\\/#organization\"},\"keywords\":[\"AWS SDK\",\"Lambda\",\"Python\",\"S3\"],\"articleSection\":[\"AWS Tutorial\",\"Lambda\",\"S3\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.maghilda.com\\\/staging\\\/9669\\\/lambda-with-s3-trigger-using-aws-sdk-and-python\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.maghilda.com\\\/staging\\\/9669\\\/lambda-with-s3-trigger-using-aws-sdk-and-python\\\/\",\"url\":\"https:\\\/\\\/www.maghilda.com\\\/staging\\\/9669\\\/lambda-with-s3-trigger-using-aws-sdk-and-python\\\/\",\"name\":\"HOWTO: Lambda with S3 trigger using AWS SDK and python - maghilda\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.maghilda.com\\\/staging\\\/9669\\\/#website\"},\"datePublished\":\"2023-12-27T19:23:38+00:00\",\"dateModified\":\"2024-01-07T23:52:03+00:00\",\"description\":\"Invoke Lambda function with S3 trigger using AWS SDK and Python\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.maghilda.com\\\/staging\\\/9669\\\/lambda-with-s3-trigger-using-aws-sdk-and-python\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.maghilda.com\\\/staging\\\/9669\\\/lambda-with-s3-trigger-using-aws-sdk-and-python\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.maghilda.com\\\/staging\\\/9669\\\/lambda-with-s3-trigger-using-aws-sdk-and-python\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.maghilda.com\\\/staging\\\/9669\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"HOWTO: Lambda with S3 trigger using AWS SDK and python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.maghilda.com\\\/staging\\\/9669\\\/#website\",\"url\":\"https:\\\/\\\/www.maghilda.com\\\/staging\\\/9669\\\/\",\"name\":\"maghilda.com\",\"description\":\"Technology blog focused on cloud computing, emerging technologies, software development and security.\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.maghilda.com\\\/staging\\\/9669\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.maghilda.com\\\/staging\\\/9669\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.maghilda.com\\\/staging\\\/9669\\\/#organization\",\"name\":\"maghilda.com\",\"url\":\"https:\\\/\\\/www.maghilda.com\\\/staging\\\/9669\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.maghilda.com\\\/staging\\\/9669\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.maghilda.com\\\/staging\\\/9669\\\/wp-content\\\/uploads\\\/2023\\\/12\\\/cropped-logo_red.png\",\"contentUrl\":\"https:\\\/\\\/www.maghilda.com\\\/staging\\\/9669\\\/wp-content\\\/uploads\\\/2023\\\/12\\\/cropped-logo_red.png\",\"width\":512,\"height\":512,\"caption\":\"maghilda.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.maghilda.com\\\/staging\\\/9669\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.maghilda.com\\\/staging\\\/9669\\\/#\\\/schema\\\/person\\\/21009c5e4f1817ea18c81d5004bcec1e\",\"name\":\"vibs\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/28233c799bf0736fecb2854057b69e52d9bd97b467b55be3406890936003faee?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/28233c799bf0736fecb2854057b69e52d9bd97b467b55be3406890936003faee?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/28233c799bf0736fecb2854057b69e52d9bd97b467b55be3406890936003faee?s=96&d=mm&r=g\",\"caption\":\"vibs\"},\"sameAs\":[\"https:\\\/\\\/www.maghilda.com\\\/staging\\\/9669\"],\"url\":\"https:\\\/\\\/www.maghilda.com\\\/staging\\\/9669\\\/author\\\/obliczte\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"HOWTO: Lambda with S3 trigger using AWS SDK and python - maghilda","description":"Invoke Lambda function with S3 trigger using AWS SDK and Python","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.maghilda.com\/staging\/9669\/lambda-with-s3-trigger-using-aws-sdk-and-python\/","og_locale":"en_US","og_type":"article","og_title":"HOWTO: Lambda with S3 trigger using AWS SDK and python - maghilda","og_description":"Invoke Lambda function with S3 trigger using AWS SDK and Python","og_url":"https:\/\/www.maghilda.com\/staging\/9669\/lambda-with-s3-trigger-using-aws-sdk-and-python\/","og_site_name":"maghilda","article_published_time":"2023-12-27T19:23:38+00:00","article_modified_time":"2024-01-07T23:52:03+00:00","author":"vibs","twitter_card":"summary_large_image","twitter_misc":{"Written by":"vibs","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.maghilda.com\/staging\/9669\/lambda-with-s3-trigger-using-aws-sdk-and-python\/#article","isPartOf":{"@id":"https:\/\/www.maghilda.com\/staging\/9669\/lambda-with-s3-trigger-using-aws-sdk-and-python\/"},"author":{"name":"vibs","@id":"https:\/\/www.maghilda.com\/staging\/9669\/#\/schema\/person\/21009c5e4f1817ea18c81d5004bcec1e"},"headline":"HOWTO: Lambda with S3 trigger using AWS SDK and python","datePublished":"2023-12-27T19:23:38+00:00","dateModified":"2024-01-07T23:52:03+00:00","mainEntityOfPage":{"@id":"https:\/\/www.maghilda.com\/staging\/9669\/lambda-with-s3-trigger-using-aws-sdk-and-python\/"},"wordCount":754,"commentCount":0,"publisher":{"@id":"https:\/\/www.maghilda.com\/staging\/9669\/#organization"},"keywords":["AWS SDK","Lambda","Python","S3"],"articleSection":["AWS Tutorial","Lambda","S3"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.maghilda.com\/staging\/9669\/lambda-with-s3-trigger-using-aws-sdk-and-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.maghilda.com\/staging\/9669\/lambda-with-s3-trigger-using-aws-sdk-and-python\/","url":"https:\/\/www.maghilda.com\/staging\/9669\/lambda-with-s3-trigger-using-aws-sdk-and-python\/","name":"HOWTO: Lambda with S3 trigger using AWS SDK and python - maghilda","isPartOf":{"@id":"https:\/\/www.maghilda.com\/staging\/9669\/#website"},"datePublished":"2023-12-27T19:23:38+00:00","dateModified":"2024-01-07T23:52:03+00:00","description":"Invoke Lambda function with S3 trigger using AWS SDK and Python","breadcrumb":{"@id":"https:\/\/www.maghilda.com\/staging\/9669\/lambda-with-s3-trigger-using-aws-sdk-and-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.maghilda.com\/staging\/9669\/lambda-with-s3-trigger-using-aws-sdk-and-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.maghilda.com\/staging\/9669\/lambda-with-s3-trigger-using-aws-sdk-and-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.maghilda.com\/staging\/9669\/"},{"@type":"ListItem","position":2,"name":"HOWTO: Lambda with S3 trigger using AWS SDK and python"}]},{"@type":"WebSite","@id":"https:\/\/www.maghilda.com\/staging\/9669\/#website","url":"https:\/\/www.maghilda.com\/staging\/9669\/","name":"maghilda.com","description":"Technology blog focused on cloud computing, emerging technologies, software development and security.","publisher":{"@id":"https:\/\/www.maghilda.com\/staging\/9669\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.maghilda.com\/staging\/9669\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.maghilda.com\/staging\/9669\/#organization","name":"maghilda.com","url":"https:\/\/www.maghilda.com\/staging\/9669\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.maghilda.com\/staging\/9669\/#\/schema\/logo\/image\/","url":"https:\/\/www.maghilda.com\/staging\/9669\/wp-content\/uploads\/2023\/12\/cropped-logo_red.png","contentUrl":"https:\/\/www.maghilda.com\/staging\/9669\/wp-content\/uploads\/2023\/12\/cropped-logo_red.png","width":512,"height":512,"caption":"maghilda.com"},"image":{"@id":"https:\/\/www.maghilda.com\/staging\/9669\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.maghilda.com\/staging\/9669\/#\/schema\/person\/21009c5e4f1817ea18c81d5004bcec1e","name":"vibs","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/28233c799bf0736fecb2854057b69e52d9bd97b467b55be3406890936003faee?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/28233c799bf0736fecb2854057b69e52d9bd97b467b55be3406890936003faee?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/28233c799bf0736fecb2854057b69e52d9bd97b467b55be3406890936003faee?s=96&d=mm&r=g","caption":"vibs"},"sameAs":["https:\/\/www.maghilda.com\/staging\/9669"],"url":"https:\/\/www.maghilda.com\/staging\/9669\/author\/obliczte\/"}]}},"_links":{"self":[{"href":"https:\/\/www.maghilda.com\/staging\/9669\/wp-json\/wp\/v2\/posts\/1247","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.maghilda.com\/staging\/9669\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.maghilda.com\/staging\/9669\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.maghilda.com\/staging\/9669\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.maghilda.com\/staging\/9669\/wp-json\/wp\/v2\/comments?post=1247"}],"version-history":[{"count":5,"href":"https:\/\/www.maghilda.com\/staging\/9669\/wp-json\/wp\/v2\/posts\/1247\/revisions"}],"predecessor-version":[{"id":2362,"href":"https:\/\/www.maghilda.com\/staging\/9669\/wp-json\/wp\/v2\/posts\/1247\/revisions\/2362"}],"wp:attachment":[{"href":"https:\/\/www.maghilda.com\/staging\/9669\/wp-json\/wp\/v2\/media?parent=1247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.maghilda.com\/staging\/9669\/wp-json\/wp\/v2\/categories?post=1247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.maghilda.com\/staging\/9669\/wp-json\/wp\/v2\/tags?post=1247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}