Làm bài EventBridge + Lambda + SNS Lab này thôi
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
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.
- Go to lambda dashboard and click create function
- Keep as Author from scratch and Name the function
- Select Runtime Python 3.X (whatever you want)
- Permissions, use an existing role that you created before
- 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.
Enter a key and value so the Lamba Function can work perfectly.
- 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.
- INSTANCE_TYPE
- Key: INSTANCE_TYPE
- Value: t2.micro
- KEY_NAME
- Key: KEY_NAME
- Value: The name of the EC2 key pair you created earlier.
- REGION
- Key: REGION
- Value: The name of the Region.
- 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
- Name the Rule
- Choose A schedule that runs at a regular rate, such as every 15 minutes. and enter the rate expression.
- For Target types, choose AWS service. For Select a target, choose Lambda function from the drop-down list.
- 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)
- Open the Amazon SNS console.
- On the navigation pane, choose Topics.
- Choose Create topic.
- In the Details section, for Type, choose Standard.
- For Name, enter the name of your topic.
- Choose Create topic.
- From the created topic, choose Create subscription.
- For Topic ARN, select the Amazon Resource Name (ARN) of the topic that you created if not automatically populated.
- For Protocol, enter Email.
- For Endpoint, enter the email address where you want to receive SNS notifications.
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
- Name the Rule
- Choose rule with an Event pattern. Under Service Name select EC2 and under Event Type select EC2 Spot Instance Interruption Warning
- In Select targets, choose SNS target so the EventBridge can publish notification
- Since the Exam also ask you to use the Input Tranformer. Click Additional settings
- Define Input path
{ "instance": "$.detail.instance", "state": "$.detail.state" }
- Define Template
"Instance <instance> is in <state>"
- Create Additional targets for Lambda. Select a target, choose Lambda function from the drop-down list.
- 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" } }