Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

Setting Up Lambda Functions with S3 Event Triggers

Lambda event triggers are extremely useful for automating serverless workflow, as they help trigger Lambda code/logic and have use cases from monitoring to processing online purchase orders and emailing receipts. In this lab, you'll create a Lambda function from scratch and create an S3 event trigger to execute the Lambda logic.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 1h 0m
Published
Clock icon Oct 18, 2019

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Create an IAM Role for Lambda

    1. Change to the directory where the necessary files are located:

      cd exercise_files/Section4-AppLayer/Lab1-LambdaS3EventTrigger/
      
    2. Create an IAM role for Lambda using the AWS IAM CLI command:

      aws iam create-role --role-name LambdaIAMRole --description "Lambda Role" --assume-role-policy-document file://lambda_assume_role_policy.json
      
    3. In the output under "Arn", copy the role ARN and paste it into a text file for later use.

  2. Challenge

    Create a Policy for the Lambda Function and Attach It to Role

    1. Create an IAM policy:

      aws iam create-policy --policy-name LambdaRolePolicy --policy-document file://lambda_execution_policy.json
      
    2. In the output under "Arn", copy the policy ARN for use in the next set of commands.

    3. Attach the policy to the role, replacing <POLICY_ARN> with the ARN previously copied:

      aws iam attach-role-policy --role-name "LambdaIAMRole" --policy-arn <POLICY_ARN>
      
  3. Challenge

    Create an SNS Topic and Subscribe Your Email Address to It

    1. Create an SNS topic:

      aws sns create-topic --name LambdaTopic --region us-east-1
      
    2. In the output under "TopicArn", copy the topic ARN for use in the next set of commands.

    3. Subscribe an endpoint (e.g., email address) to your topic, replacing <TOPIC_ARN> with the previously copied ARN and <EMAIL_ADDRESS> with your own email:

      aws sns subscribe --protocol "email" --topic-arn <TOPIC_ARN> --notification-endpoint <EMAIL_ADDRESS> --region us-east-1
      

      You should receive the status message "SubscriptionArn": "pending confirmation".

    4. To confirm the subscription, access the email previously used, open the SNS email, and click click Confirm subscription.

  4. Challenge

    Modify the Lambda Function with the SNS Topic ARN and Zip It into a Lambda Deployment Package

    1. Open the lambda_function.py file:

      vim lambda_function.py
      
    2. To enable sending SNS notifications, uncomment the line client = boto3.client('sns') and the section below:

      response = client.publish(
      TopicArn='<SNS-TOPIC-ARN>',
      Message= payload_str,
      Subject='My Lambda S3 event')
      
    3. In TopicArn, replace with the topic ARN previously copied.

    4. To save and exit the file, press ESC, type :wq, and press Enter.

    5. Zip the file into a deployment package:

      zip lambda_function.zip lambda_function.py
      
  5. Challenge

    Create a Lambda Function

    1. Create a Lambda function, replacing <ROLE_ARN> with the role ARN previously copied:
    aws lambda create-function --memory-size 128 --function-name my-lambda --runtime python3.7 --handler lambda_function.lambda_handler --zip-file fileb://lambda_function.zip --role <ROLE_ARN>
    
    1. In the output under "FunctionArn", copy the function ARN to a text file for later use.
  6. Challenge

    Add Lambda Permission for the S3 Service to Invoke the Function

    1. Add Lambda permission, replacing <ARN_S3_BUCKET> with the ARN of the S3 bucket provided on the lab credentials page:
    aws lambda add-permission --action lambda:InvokeFunction --principal s3.amazonaws.com --statement-id LabS3Trigger --function-name my-lambda --source-arn "<ARN_S3_BUCKET>"
    
  7. Challenge

    Enable and Add Notification Configuration to the S3 Bucket

    1. Open the bucket-trigger-notification.json file:

      vim bucket-trigger-notification.json
      
    2. In the output under "LambdaFunctionArn", delete the existing text and replace it with the function ARN previously copied into a text file:

      "LambdaFunctionArn": "<FUNCTION_ARN>"
      
    3. To save and exit the file, press ESC, type :wq, and press Enter.

    4. Enable the notification configuration on the S3 website bucket, replacing <S3_BUCKET_NAME> with the bucket name provided on the lab credentials page:

      aws s3api put-bucket-notification-configuration --bucket <S3_BUCKET_NAME> --notification-configuration file://bucket-trigger-notification.json
  8. Challenge

    Verify Configuration by Uploading a File to the Provided S3 Bucket

    1. Upload a file to the bucket, replacing <S3_BUCKET_NAME> with the bucket name provided on the lab credentials page:

      aws s3 cp lambda_function.py s3://<S3_BUCKET_NAME>
      
    2. Once successfully uploaded, check your email.

      You should receive a notification email with details of the file uploaded to the S3 bucket.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans