AWS 177-[SECO]-Lab - [Challenge] AWS Lambda Exercise

Описание к видео AWS 177-[SECO]-Lab - [Challenge] AWS Lambda Exercise

#AWS 177-[SECO]-Lab - [Challenge] AWS Lambda Exercise

Check out my GitHub Repository - https://github.com/MFMKURIA/More-AWS-...
Portfolio
1. http://markfrancismk.sciawareness.com...
2. https://d2taxcp5hluc5o.cloudfront.net/
Let’s go through the AWS Lambda exercise step-by-step. This will cover creating a Lambda function to count the number of words in a text file, setting up an S3 bucket to trigger this Lambda function, and using SNS to send the word count result via email. I’ll also include potential exam questions and answers at the end.

---

AWS Lambda Exercise: Detailed Step-by-Step Guide

Lab Overview:
You will create an AWS Lambda function to count the number of words in a text file, configure an S3 bucket to invoke this function upon file upload, and set up an SNS topic to report the word count via email.

---

1. Launch Your Lab Environment

Step 1: Click on “Start Lab” at the top of the instructions page.

Step 2: Wait until the message “Lab status: ready” appears. This indicates that your AWS environment is set up and ready for use.

Step 3: Close the Start Lab panel by choosing the X in the upper-right corner.

Purpose: This initializes a new AWS environment where you can create and test your Lambda function and associated resources.

---

2. Create an S3 Bucket

Step 1: Open the AWS Management Console.

Step 2: Navigate to S3 (Simple Storage Service) by typing “S3” in the search bar and selecting it.

Step 3: Click on Create bucket.

Step 4: Enter a unique name for your bucket (e.g., `my-word-count-bucket`).

Step 5: Choose the same AWS Region where you will create your Lambda function.

Step 6: Click on Create bucket.

Purpose: The S3 bucket will store text files and trigger the Lambda function when a new file is uploaded.

---

3. Create an SNS Topic

Step 1: In the AWS Management Console, navigate to SNS (Simple Notification Service).

Step 2: Click on Create topic.

Step 3: Choose Standard as the type of topic.

Step 4: Enter a name for your topic (e.g., `WordCountTopic`).

Step 5: Click on Create topic.

Purpose: This SNS topic will be used to send notifications (email or SMS) with the word count results.

---

4. Create a Lambda Function

Step 1: In the AWS Management Console, navigate to Lambda.

Step 2: Click on Create function.

Step 3: Choose Author from scratch.

Step 4: Enter a name for your function (e.g., `WordCountFunction`).

Step 5: Choose Python 3.x as the runtime.

Step 6: Under Permissions, choose Use an existing role and select `LambdaAccessRole` from the dropdown.

Step 7: Click on Create function.

Purpose: This Lambda function will process the files uploaded to the S3 bucket and count the number of words.

---

5. Add Code to Lambda Function

Step 1: In the Lambda function details page, scroll down to the Function code section.

Step 2: Replace the default code with the following Python code:

```python
import json
import boto3

s3 = boto3.client('s3')
sns = boto3.client('sns')

def lambda_handler(event, context):
Extract the S3 bucket name and object key from the event
bucket = event['Records'][0]['s3']['bucket']['name']
key = event['Records'][0]['s3']['object']['key']

Download the file from S3
response = s3.get_object(Bucket=bucket, Key=key)
text = response['Body'].read().decode('utf-8')

Count the number of words in the file
word_count = len(text.split())

Create the message to send
message = f"The word count in the {key} file is {word_count}."

Publish the message to SNS
sns.publish(
TopicArn='arn:aws:sns:YOUR_REGION:YOUR_ACCOUNT_ID:WordCountTopic',
Subject='Word Count Result',
Message=message
)

return {
'statusCode': 200,
'body': json.dumps('Word count processed successfully')
}
```

Explanation:
`boto3.client('s3')`: Initializes the S3 client to interact with S3.
`boto3.client('sns')`: Initializes the SNS client to send notifications.
`event`: Contains details about the S3 event that triggered the function.
`s3.get_object`: Downloads the file from S3.
`text.split()`: Splits the text into words.
`sns.publish`: Sends a notification with the word count.

Step 3: Click Deploy to save your changes.

Purpose: This code processes the text file, counts the words, and sends a notification with the result.

---

6. Configure S3 Trigger for Lambda

Step 1: Go back to the S3 console.

Step 2: Click on the bucket you created.

Step 3: Go to the Properties tab.

Step 4: Scroll down to the Event notifications section and click on Create event notification.

Step 5: Enter a name for the notification (e.g., `WordCountNotification`).

Step 6: Under Event types, select All object create events.

Step 7: In the Destination section, select Lambda function and choose your `WordCountFunction`.

Step 8: Click Save changes.

Purpose: This sets up the S3 bucket to trigger the Lambda function whenever a new file is uploaded.

---

7. Test Your Lambda Function

Step 1: Upload a text file to your

Комментарии

Информация по комментариям в разработке