Open satish8450 opened 1 year ago
This policy should be attach to role
{ "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": [ "ec2:Start", "ec2:Stop", "ec2:DescribeInstanceStatus" ], "Resource": "" }, { "Sid": "VisualEditor1", "Effect": "Allow", "Action": [ "logs:CreateLogStream", "logs:CreateLogGroup", "logs:PutLogEvents" ], "Resource": "arn:aws:logs:::" } ] }
ref : https://gist.github.com/amitavroy/26089061c7037e1ade77497504241618
import boto3 region = 'us-east-1' instances = ['i-001e2d04e37ccde3b'] ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context): print('Starting instances') ec2.start_instances(InstanceIds=instances)
import boto3 region = 'us-east-1' instances = ['i-001e2d04e37ccde3b'] ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context): print('Stopping instances') ec2.stop_instances(InstanceIds=instances)
================================================================
By creating rules in cloudwatch events we can trigger this functions at scheduled timings 56 3 ? MON-SAT
Using cloudwatch to trigger lambda functions
for triggering of any Lambda function automatically we need to use COULD WATCH service
goto cloudwatch -- create rule -- give cron expression
in Target group choose lambda function Any that will trigger .
If you choose EC2start function that will trigger by using cron expression
Youtube links :
EC2 start Stop https://www.youtube.com/watch?v=Yq-z8yT7Aq0
LAMDA full tutorial https://www.youtube.com/watch?v=sBkoT6_xK1Q
Lambda: Run code without thinking about servers
This is serverless acrchiture used to run any scripts without servers, we can strat and stop the ec2 instances or we can creat any tables in DynamoDb
in lambda we will create functions for each events
Create function : Author from scratch : customised fucntions purpose will use this Use a blueprint : this kind of functions are predefined by AWS Container image : we can deploy images
Author from scratch :
Function name : name of the function Runtime : the platform which can support your code Architecture : Choose the instruction set architecture you want for your function code. Create function : function created here we can see how function looks like
We need to create event for function
Goto test Test event action : Create new event Event name : test Event sharing settings : private Event JSON : here we need define our code
click on test : code will execute , by default test results will store in CloudWatch logs
add trigger : this we can use to trigger the particular action needs to be done after test passed add destination : we use this the final output showcase we can attach SNS here to get notification