AWS EC2 Instance with Lambda & Event Bridge

I'm Security and Cloud Support Engineer at KBZ Bank. I'm tying to be a Cloud Native Engineer......
Hello Guys! I’m Back. Sharing မလုပ်ဖြစ်တာတော့ တော်တော်ကြာသွားပါပြီ။ Personal Case and Emotion တွေကြောင့်ရော အလုပ်မှာ အရမ်း ပင်ပန်းနေတာတွေ ကြောင့်ပါ။ ဆင်ခြေပေးတာပါ။
အခု sharing လုပ်ပေးချင်တာကလဲ အရင်က ကျွန်တော် sharing လုပ်ပေးဖူးတဲ့ အရာပါပဲ။ EC2 instance ကို schedule လုပ်တာပါ။ ဒါပေမယ့် လုပ်ပုံလုပ်နည်းတော့ ကွဲသွားပါတယ်။
အခုလုပ်ပြမှာက AWS Lambda Function မှာ python ရဲ့ boto3 ကို သုံးပြီး တော့ schedule လုပ်မှာပါ။ ကျွန်တော် production run ဖြစ်သွားတဲ့ နည်းလဲ ဖြစ်ပါတယ်။
ဒါဆို စလိုက်ပါတော့မယ်။
အောက်က instance လေးကို Mon-Fri 8:00 AM to 6:00 PM ပဲ up and running ဖြစ်အောင် schedule လုပ်မှာ ဖြစ်ပါတယ်။

Creating IAM Role
ပထမဆုံး ကျွန်တော် Lambda အတွက်သုံးမယ့် IAM Role တစ်ခုဆောက်ပါမယ်။ Role မှာ ကပ်ဖို့ Policy လဲပါမှာတော့။ အောက်က အတိုင်း Policy တစ်ခုဆောက်ပြီး Role Permission မှာ Policy ကို add ပေးလိုက်ပါမယ်။
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": [
"ec2:Start*",
"ec2:Stop*"
],
"Resource": "*"
}
]
}


Creating Lambda Function For EC2 Start and Stop
ပြီးရင်တောာ့ Lambda Function 2 ခု ဆောက်ပါမယ်။ တစ်ခုကို start အတွက်သုံးပြီး တစ်ခုကို stop အတွက် သုံးပါမယ်။ အခု ကျွန်တော် သုံးပြထားတာထက် ပိုအဆင်ပြေတဲ့ Lambda Function လေး တွေ့ရင်လဲ တစ်ချက်လောက် ပြောပြပေးခဲ့ပါဦးနော်။
EC2 Start Lambda Function
Create Function ကို ရွေးပြီး ကျွန်တော်တို့ Function တစ်ခု ဆောက်ပါမယ်

Function အတွက် Name ပေးမယ် ပြီးတော့ ကြိုပြီး ဆောက်ထားတဲ့ IAM Policy လေးကို ကပ်ပေးပါမယ်။ Runtime ကိုတော့ Python ကို ရွေးပေးရပါမယ်။


Advanced Settings ကိုတော့ ကျွန်တော် မထိတော့ပါဘူး။
Click on Create Function. ဒါဆိုရင် ကျွန်တော်အောက်က Python Code ကို ထည့်လိုက်ပါမယ်။
import boto3
# change the region name below
region = 'ap-southeast-1'
#change the Instance ID
instances = ['i-024b488a473052092']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
ec2.start_instances(InstanceIds=instances)
print('started your instances: ' + str(instances))
ပြီးရင် ကျွန်တော် Ctrl + S save လုပ်မယ်။ ပြီးရင် Deploy လုပ်ပါမယ်။

ကျွန်တော် စမ်းခဲ့ ရသလောက်တော့ Code ရဲ့ Timeout Setting ကို 10s ပြောင်းလိုက်တာ ပို အဆင်ပြေတယ်ဗျ။ ကျွန်တော်တို့ တစ်ခါထည်း ပြောင်းလိုက်ပါမယ်။


ပြီးရင်တော့ ကျွန်တော်တို့ Save လုပ်ပြီးတော့ Stop Function အတွက် ဆက်လုပ်ပါမယ်။
EC2 Stop Lambda Function
အဆင့်တွေက အကုန် အတူတူပဲမို့ ကျွန်တော် စာကို ခြုံသောအားဖြင့် အကုန် မလုပ်ပြတော့ပါဘူး။ Code ပဲ ပြောင်းလဲမူ ရှိပါတယ်။ Try ကြည့်လိုက်ပါဗျ။
import boto3
# change the region name below
region = 'ap-southeast-1'
#change the Instance ID
instances = ['i-024b488a473052092']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
ec2.stop_instances(InstanceIds=instances)
print('stopped your instances: ' + str(instances))
multiple instance အတွက်ဆိုရင် Programming သဘော တရားအတိုင်းပဲ comma ခံပြီး သုံးလို့ရပါတယ်။
#change the Instance ID
instances = ['i-024b488a473052092','i-iloveyou3000']
Lambda Function 2 ခု ရပြီဆိုတော့ 70% တော့ အဆင်ပြေနေပြီဗျ။

Amazon Event Bridge For Instance Schedule
Linux မှာ cron ဆိုတာရှိတယ်ဗျ။ ဘယ်လိုလဲဆိုတော့ basic automation ပေါ့ဗျာ။ ကိုယ်က ဒီ script ကို ဒီအချိန်ရောက်ရင် auto run စေချင်တယ်။ အဲ့လို အခါမျိုးမှာ သုံးတာပေါ့။
အခုလဲ ကျွန်တော်လုပ်မှာက မနက် ၈ နာရီဆိုရင် demo-instance-start-function ဆိုတာလေးကို Run ပြီး ညနေရုံးဆင်းရင် demo-instance-stop-function ဆိုတဲ့ Function လေးကို Run မှာဆိုတော့ Amazon Event Bridge 2 ခုဆောက်ရပါမယ်။
Cron ကိုတော့ Crontag Guru မှာ ကြည့်လို့လဲ ရပါတယ်။
ဒါဆို ကျွန်တော်တို့ Start Function အတွက် စဆောက်လိုက်ရအောင်။



Schedule Pattern မှာတော့ ကျွန်တော်တို့က အမြဲ သုံးမှာမို့ Recurring schedule ကို ရွေးပါတယ်။ Time Zone ကိုလဲ ကိုယ် လုပ်ချင်တဲ့ အချိန်ပေါ့။ Cron Expression ကိုတော့ ကျွန်တော် ထည့်သလိုထည့်လိုက်ပါ။

အောက်မှာလဲ script run မယ့် အချိန်လေးတွေကို ပြပေးပါတယ်။
Flexible time window ကိုတော့ Off ထားခဲ့လို့ ရပါတယ်။ သူက ဘယ်လိုလဲ ဆိုတော့ ကိုယ်က 5 mins ရွေးထားရင် 8 နာရီ cron tab run ထားပေမယ့် flexible time windows 5 mins ကြောင့် 8 နာရီကနေ 8:05 အတွင်း အဆင်ပြေတဲ့ အချိန်မှာ run မှာဖြစ်ပါတယ်။

Target မှာတော့ Lambda Function ကို ရွေးပါမယ်။




Permission ကတော့ Default အတိုင်းသုံးလို့လဲ ရပါတယ်။ Default အတိုင်း သုံးမယ်ဆိုရင် Start အတွက် တစ်ခု Stop အတွက် တစ်ခု ရမှာပါ။ ကျွန်တော်ကတော့ Start အတွက် တော့ အသစ်ဆောက်ခိုင်းလိုက်ပြီး edit လုပ်ပြီး stop အတွက်ပါ ပေါင်းထည့်ပေးလိုက်ပါတယ်။ ဒါကတော့ Optional ဖြစ်လို့ အဆင်ပြေသလို လုပ်လို့ရပါတယ်။
အောက်မှာပြထားပေးပါတယ်။
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"lambda:InvokeFunction"
],
"Resource": [
"arn:aws:lambda:ap-southeast-1:339712713779:function:demo-instance-start-function:*",
"arn:aws:lambda:ap-southeast-1:339712713779:function:demo-instance-start-function",
"arn:aws:lambda:ap-southeast-1:339712713779:function:demo-instance-stop-function:*",
"arn:aws:lambda:ap-southeast-1:339712713779:function:demo-instance-stop-function"
]
}
]
}
ဒါဆို ရင် Stop အတွက်လဲ ထပ်ဆောက်ရပါမယ်။ Step တွေက တူတူ ပဲမို့ လုပ်ကြည့်လိုက်ပါ။ ပြောင်းပေးရမှာက Cron Expression ပါပဲ။
00 18 ? Mon-Fri


Schedule 2 ခုလဲ ရပြီ ကျွန်တော်တို့ရဲ့ IAM Policy and Role တွေကလဲ မှန်တယ်ဆိုရင် ကျွန်တော်တို့ရဲ့ instance လေးက မနက် ၈ နာရီဆို start လုပ်ပြီး ညနေ၆ နာရီဆိုရင် stop ဖြစ်မှာပါ။ Cost saving တော့ တော်တော် ဖြစ်ပါတယ်။
လိုအပ်ချက်လေးတွေရှိရင် ပြောပြခဲ့ပေးပါဦးနော်။
the-art-by-makisam






