EventBridge + Lambda + SNS Lab

Làm bài EventBridge + Lambda + SNS Lab này thôi

Share Everywhere

Table of contents

It's already have 2 lambda Funtion and the lab ask you to:

  • Using EventBridge to run the first lamda function every 15 minutes
  • Setup SNS Topic (Without Subscribe) and the Second Lambda Function it's running a new Spot Instance if one Spot Instance got terminated (You cant view the Lambda Function). And using EventBridge to trigger SNS topic

Prepare

In the Exam 2 Lambda Function are already there so for simulate you have to create 2 Lambda Function on your own.

Let's navigate to AWS Identity and Access Management (IAM) to create a role for our AWS Lambda function. Select Role > Create Role > Select AWS service > Select Lambda and give Lambda EC2 Full Access

It's already have 2 lambda Funtion and the lab ask you to:

Or you can create a role with the below policy.

{
    "Version": "2012-10-17",
    "Statement": [
        {
        "Effect": "Allow",
        "Action": [
            "logs:CreateLogGroup",
            "logs:CreateLogStream",
            "logs:PutLogEvents"
        ],
        "Resource": "arn:aws:logs:*:*:*"
        },
        {
            "Sid": "EC2FULLACCESS",
            "Effect": "Allow",
            "Action": [
                "ec2:*"
            ],
            "Resource": "*"
    ]
}

Next we creating 2 Lambda Function.

  1. Go to lambda dashboard and click create function
  2. Keep as Author from scratch and Name the function
  3. Select Runtime Python 3.X (whatever you want)
  4. Permissions, use an existing role that you created before
  5. Goto code and paste: HelloWorld.py and spotHandler.py

For spotHandler you need one more step to make it's work

Choose Configuration -> Environment variables -> Edit -> Add environment variable.

It's already have 2 lambda Funtion and the lab ask you to:

Enter a key and value so the Lamba Function can work perfectly.

  1. AMI
  • Key: AMI
  • Value: Open EC2 in a new browser tab, click Launch Instance and copy and paste the ami value listed after Amazon Linux 2.
  1. INSTANCE_TYPE
  • Key: INSTANCE_TYPE
  • Value: t2.micro
  1. KEY_NAME
  • Key: KEY_NAME
  • Value: The name of the EC2 key pair you created earlier.
  1. REGION
  • Key: REGION
  • Value: The name of the Region.
  1. SUBNET_ID
  • Key: SUBNET_ID
  • Value: Navigate to VPC > Subnets, and copy and paste the ID of one of the public subnets in your VPC.

To test the lambda function, Choose the function you have created. Choose Actions and Create a test events by clicking configure test events

Start the Lab

There are two requirements:

  • Create a Schedule Rule that trigger Lambda Function every 15 minutes
  • Create a Rule that will pulish sns notification and also trigger a Lambda Function when a Spot Instace got Terminate

First requirement:

Goto EventBridge from AWS console and Click Create rule

  1. Name the Rule
It's already have 2 lambda Funtion and the lab ask you to:
  1. Choose A schedule that runs at a regular rate, such as every 15 minutes. and enter the rate expression.
It's already have 2 lambda Funtion and the lab ask you to:
  1. For Target types, choose AWS service. For Select a target, choose Lambda function from the drop-down list.
It's already have 2 lambda Funtion and the lab ask you to:
  1. Click Next then you done the first requirement.

Second requirement:

Create an SNS topic and subscription (Since the Exam not ask you to crate a subscription so you don't have to do it)

  1. Open the Amazon SNS console.
  2. On the navigation pane, choose Topics.
  3. Choose Create topic.
  4. In the Details section, for Type, choose Standard.
  5. For Name, enter the name of your topic.
  6. Choose Create topic.
It's already have 2 lambda Funtion and the lab ask you to:
  1. From the created topic, choose Create subscription.
  2. For Topic ARN, select the Amazon Resource Name (ARN) of the topic that you created if not automatically populated.
  3. For Protocol, enter Email.
  4. For Endpoint, enter the email address where you want to receive SNS notifications.
It's already have 2 lambda Funtion and the lab ask you to:

You now have an SNS topic that EventBridge Events can send the EC2 Spot Interruption Notification to, let’s configure EventBridge to do so.

Goto EventBridge from AWS console and Click Create rule

  1. Name the Rule
It's already have 2 lambda Funtion and the lab ask you to:
  1. Choose rule with an Event pattern. Under Service Name select EC2 and under Event Type select EC2 Spot Instance Interruption Warning
It's already have 2 lambda Funtion and the lab ask you to:
  1. In Select targets, choose SNS target so the EventBridge can publish notification
It's already have 2 lambda Funtion and the lab ask you to:
  1. Since the Exam also ask you to use the Input Tranformer. Click Additional settings
It's already have 2 lambda Funtion and the lab ask you to:
  1. Define Input path
{
    "instance": "$.detail.instance",
    "state": "$.detail.state"
}
  1. Define Template
    "Instance <instance> is in <state>"
  1. Create Additional targets for Lambda. Select a target, choose Lambda function from the drop-down list.
It's already have 2 lambda Funtion and the lab ask you to:
  1. Click Next then you done the second requirement.

You can test by request a new spot fleet then terminate the request, then there will be a notification to your mail look like this:

{
  "version": "0",
  "id": "6009a9f4-cc7a-8a77-46f2-310520b31e0f",
  "detail-type": "EC2 Spot Instance Interruption Warning",
  "source": "aws.ec2",
  "account": "<account-id>",
  "time": "2019-05-27T04:52:57Z",
  "region": "eu-west-1",
  "resources": [
    "arn:aws:ec2:eu-west-1b:instance/i-0481ef86f172b68d7"
  ],
  "detail": {
    "instance-id": "i-0481ef86f172b68d7",
    "instance-action": "terminate"
  }
}
Bạn thấy bài viết này như thế nào?
2 reactions

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Bài viết liên quan

Khám phá FinOps

Khám phá FinOps - công nghệ đám mây

Nhiều doanh nghiệp ngày nay lựa chọn chuyển sang công nghệ đám mây với hi vọng đạt được lợi thế cạnh tranh so với đối thủ nhờ tiềm năng về hiệu quả cao và tiết kiệm chi phí hơn của công nghệ này.
Microservices Roadmap

Microservices Roadmap

- Kafka, RabbitMQ, Amazon SQS: Efficient and reliable message brokers for seamless communication between microservices.
The Data Analyst Roadmap

The Data Analyst Roadmap

**Database Knowledge**: Gain proficiency in working with databases like MySQL, PostgreSQL, or MongoDB.
Architectural patterns in software design

Architectural patterns in software design

Choose the architecture that aligns with your application's unique needs and goals. Each pattern offers a tailored approach to elevate your software system!
Exploring the Technological Marvel Behind Netflix

Exploring the Technological Marvel Behind Netflix

Ever wondered about the tech wizardry that powers your binge-watching adventures on Netflix?