Tuesday, April 28, 2020

Activity - AWS Lambda

Note: This blog is for you to make your life easier. 
Don't make your life complicated, just enjoy the blog. ^_^
Please pardon my simplicity of my own blog and look for the content. 

To make it more easier for you just click one photos, follow then scroll. ⏩

Enjoy reading! ^_^


Activity: AWS Lambda


Overview


In this activity we will create an AWS Lambda function and will also create an Amazon CloudWatch event to trigger the function every minute. The function uses an AWS Identity and Access Management (IAM) role. This IAM role allows the function to stop an Amazon Elastic Compute Cloud (Amazon EC2) instance that is running in the Amazon Web Services (AWS) account.



1. At the top of these instructions, click Start Lab to launch your lab.

2. Wait until you see the Lab status: in creation message. To close the Start Lab panel, click the X.




3. At the top of these instructions, click AWS
Note: if you were not able to see it, check your browser and allow pop-up.


4. Arrange the AWS Management Console tab so that it displays alongside these instructions. Ideally, you will be able to see both browser tabs at the same time, which makes it easier to follow the activity steps.




Task 1: Create a Lambda function

5. In the AWS Management Console, from the Services menu, choose Lambda.

  1. Note: If you see a warning message that says tags failed to load, you can ignore it.


6. Click Create function.

7. In the Create function screen, configure these settings:
  • Choose Author from scratch
  • Function name: myStopinator
  • Runtime: Python 3.8
  • Click Choose or create an execution role
  • Execution role: Use an existing role
  • Existing role: From the dropdown list, choose myStopinatorRole


8. Click Create function.








Task 2: Configure the trigger

We will configure a scheduled event to trigger the Lambda function by setting a CloudWatch event as the event source (or trigger). The Lambda function can be configured to operate much like a cron job on a Linux server, or a scheduled task on a Microsoft Windows server. However, you do not need to have a server running to host it.

9. Click + Add trigger.


10. Click the Select a trigger dropdown menu, and choose CloudWatch Events.

11. For the rule, choose Create a new rule and configure these settings:
  • Rule name: everyMinute
  • Rule type: Schedule expression
  • Schedule expression: rate(1 minute)


12. Click Add.





Task 3: Configure the Lambda function

13. In the Designer box, click myStopinator (which is the name of your Lambda function) to display and edit the Lambda function code.


14. In the Function code box, delete the existing code. Copy the following code, and paste it in the box:

import boto3
region = '<REPLACE_WITH_REGION>'
instances = ['<REPLACE_WITH_INSTANCE_ID>']
ec2 = boto3.client('ec2', region_name=region)

def lambda_handler(event, context):
    ec2.stop_instances(InstanceIds=instances)
    print('stopped your instances: ' + str(instances))


15. Replace the <REPLACE_WITH_REGION> placeholder with the actual Region that you are using. To do this:
Click on the region on the top right corner and use the region code. For example, the region code for US East (N. Virginia) is us-east-1.
Important: Keep the single quotation marks (' ') around the Region in your code. For example, for the N. Virginia, it would be 'us-east-1'

16. Challenge section: Verify that an EC2 instance named instance1 is running in your account, and copy the instance1 instance ID.
  • Open another browser tab and go to https://console.aws.amazon.com/ec2
  • Click Instances.
    Note that an EC2 instance named instance1 exists, and that it is in a running state.
  • From the Description tab of instance1, copy the instance ID (it will start with i-)
    Note: Leave this browser tab open. You will return to it in a moment.

    17. Return to the AWS Lambda console browser tab, and replace <REPLACE_WITH_INSTANCE_ID> with the actual instance ID that you just copied.
    Important: Keep the single quotation marks (' ') around the instance ID in your code.
    Your code should now look similar to the following example. However, you might have a different value for the Region, and you will have a different value for the instance ID:
1




18. In the top-right corner, click Save.
At the top of the AWS Lambda console, you should see the message Successfully updated the function myStopinator. You might need to scroll up to see it.
Your Lambda function is now fully configured. It should attempt to stop your instance every minute.




19. Click Monitoring (the tab near the top of the page).Note that one of the charts shows you how many times your function has been invoked. There is also a chart that shows the error count and the success rate as a percentage.

Task 4: Verify that the Lambda function worked

  1. Return to the Amazon EC2 console browser tab and see if your instance was stopped.
    Tip: You can click the  refresh icon or refresh the browser page to see the change in state more quickly.







21. Try starting the instance again. What do you think will happen? The instance will be stopped again within 1 minute.




Activity complete

22. Click End Lab at the top of this page, and then to confirm that you want to end the activity, click Yes
A panel appears, with a message that indicates: DELETE has been initiated... You may close this message box now.

23. To close the panel, go to the top-right corner and click the X.



Congratulations!!! ^_^

References: 
labs.vocareum.com
AWS Site

Lesson Learned:
I have learned that AWS Lamda activity lets you run a code without any provisions. You will pay what you consume. Also, AWS Lamda can run code virtually of any application code or backend service with no administration. 

You can upload directly the code and AWS will take care of everything which requires to run and scale your code with availability. Possibly, you can also set-up code that  automatically triggers from other AWS services.

Problems encountered: Show picture below


Using AWS Lambda we will be able to manage the serverless computing services that runs in our code for the response of the events and will automatically manages the underlying computing resources for us.




Thanks for visiting my blog.


Email me at: aileen-pacia@live.nmit.ac.nz
“I can do all things through Christ who strengthens me.”
Philippians 4:13

No comments:

Post a Comment