< strong>
Implementing something like Cloud automator Try div>
background
The story of this time is "serverless architecture", "Cloud Automator", and "re: Invent" in the first place.
Serverless architecture
It's been a hot topic. Serverless architecture.
It's nice to be released from server operation, isn't it?
Cloud Automator
Cloud Automator is a SaaS tool that automates operations such as backup. It is provided by <a href="http://www.serverworks.co.jp/" target=blank"> Serverworks .
What you can do with Cloud Automator, and typical use cases for it, are as follows.
--Start EC2 instance only during business hours
--Automatically back up EBS and RDS
- It's okay to go undercover, but I won't introduce the product because the purpose of this time is not there. Cloud Automator is just a story, so I don't know
re:Invent
You announced "Python support" and "Schedule-based execution".
Many people should be excited about this news.
I wanted to make something that incorporates these two things.
What to do this time
Let's implement one of the Cloud Automator use cases introduced in ↑, "Automation of Start / Stop of EC2 instance" with Lambda.
Launch the instance at 10:00 and drop it at 19:00. By implementing this, you can avoid accidentally forgetting to delete the instance and charging a large amount of money.
Diagram
Like this.
The current Lambda can be started on a schedule basis, so take advantage of it.
The components are the following three.
--Lambda Function ... Function [1] to start at a certain time and notify the occurrence of "event"
--SNS Topic for receiving event occurrences and invoking "actions"
--Lambda Function ... Function [2] that executes "action" such as Start / Stop (or notification of execution) of the target instance
- Since it works without the SNS Topic at the bottom right, we will not handle it this time.
procedure
- Create an IAM Role
- Create a template for SNS Topic and Lambda Function
- Implemented email notification on SNS Topic
- Event source settings for Lambda Function
- Implement and test Lambda Function
- Set a timer for Lambda's Event Source
Creating an IAM Role
In this configuration, there are two types of Lambda Functions, so assign the appropriate IAM Role to them.
Function [1] uses sns: Publish,
In Function [2], let's give permissions of sns: Publish and ec2: StartInstances, ec2: StopInstances.
This time, we will create an IAM Role to be assigned to Lambda, so let's select ** Lambda for Role Type **. Don't make a mistake.
Create a template for SNS Topic and Lambda Function
Only the outside will be made first.
Create two Function [1](for firing start / stop time), one Function [2](for execution start / stop), and at least one SNS Topic.
Embed the Function code in a suitable template.
Apply the IAM Role assignment you created earlier.
Let's register Arn of Function [1] in ** Subscriptions of Topic **.
Implemented email notification on SNS Topic
You can use it to check the operation by registering your own e-mail address in the Topic created above.
This works even if it's not the worst.
After entering your email address in Create Subscription, click "Confirm Subscription" and you will receive a confirmation email. Follow the link in the text to complete your subscription registration.
Event source settings for Lambda Function
Set the event source for your Lambda Function.
First, set the Function [1] to be started on a time basis.
You can write like Cron. The time is currently UTC only. If you want to start JST at 10:00 on weekdays, it will look like the figure above.
Function [2] is the same.
Just select the SNS Topic. It will list candidate topics at the time of setting.
Implemented and tested Lambda Function
Here is an example of the code.
#Example of Lambda Function code to tell you the EC2 startup time
import json
import boto3
sns_client = boto3.client("sns")
def lambda_handler(event, context):
print("Received event: " + json.dumps(event, indent=2))
# publish to SNS Topic ...
topic_arn = "my-topic-arn"
message = dict(Action="start", InstanceIds=["instance-id"])
param = dict(TopicArn=topic_arn, Subject="subject", Message=json.dumps(message))
return sns_client.publish(**param)
It is passing information to Topic about which instance "which instance" should perform "which action (Start / Stop)". Parameter specifications
message = dict(Action="start", InstanceIds=["instance-id"])
The description here is all. It's very nonsense that the code is directly typed to the instance ID, but let's go without worrying about it.
The following is a code example of a Function that actually executes an action (Start / Stop of an instance).
import json
import boto3
ec2_client = boto3.client("ec2")
def lambda_handler(event, context):
# Get parameter
param = json.loads(event["Records"][0]["Sns"]["Message"])
# Invoke action
if param.get("Action", "") == "start":
ret = ec2_client.start_instances(InstanceIds=param.get("InstanceIds", []))
elif param.get("Action", "") == "stop":
ret = ec2_client.stop_instances(InstanceIds=param.get("InstanceIds", []))
return ret
Don't say that the parameter acquisition method is dirty. ..
Look at the parameters contained in Message and start or stop the appropriate instance.
Implementation is completed like this.
Issues and points of reflection
There are many things, but I would like to mention the ones that are of particular concern. If all can be improved, it may be useful even if it is put on site.
Instance specification is direct
The necessary parameters should be pulled from the outside. Should I at least specify a tag?
As a device to increase versatility,
It is conceivable to implement "Lambda Function that creates" Lambda Function created this time "", wrap the Function with API Gateway, and let the target instance be specified by the POST parameter. .. Metaprogramming. I want to do this someday.
Code dirty
I didn't consider error handling or anything.
About SNS Message
JSON is converted to a character string once at the stage of passing it to the parameter, but it should be a specification that can be passed as a JSON object. I haven't studied enough in this area.
Summary
I tried to automate EC2 Start / Stop using Lambda.
There is a lot of room for improvement, and I hope we can update it soon.
that's all.