Error in testing Lambda function

0

Im having error when running a lambda function to stop and start an EC2 instance. This is the guide I followed when creating the function. https://aws.amazon.com/premiumsupport/knowledge-center/start-stop-lambda-eventbridge/

Below is the error message received when testing the function.

{ "errorMessage": "name 'ap' is not defined", "errorType": "NameError", "stackTrace": [ " File "/var/lang/lib/python3.8/imp.py", line 234, in load_module\n return load_source(name, filename, file)\n", " File "/var/lang/lib/python3.8/imp.py", line 171, in load_source\n module = _load(spec)\n", " File "<frozen importlib._bootstrap>", line 702, in _load\n", " File "<frozen importlib._bootstrap>", line 671, in _load_unlocked\n", " File "<frozen importlib._bootstrap_external>", line 843, in exec_module\n", " File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed\n", " File "/var/task/lambda_function.py", line 4, in <module>\n ec2 = boto3.client('ec2', region_name=ap-southeast-1)\n" ] }

Function Logs [ERROR] NameError: name 'ap' is not defined Traceback (most recent call last):   File "/var/lang/lib/python3.8/imp.py", line 234, in load_module     return load_source(name, filename, file)   File "/var/lang/lib/python3.8/imp.py", line 171, in load_source     module = _load(spec)   File "<frozen importlib._bootstrap>", line 702, in _load   File "<frozen importlib._bootstrap>", line 671, in _load_unlocked   File "<frozen importlib._bootstrap_external>", line 843, in exec_module   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed   File "/var/task/lambda_function.py", line 4, in <module>     ec2 = boto3.client('ec2', region_name=ap-southeast-1)[ERROR] NameError: name 'ap' is not defined Traceback (most recent call last):   File "/var/lang/lib/python3.8/imp.py", line 234, in load_module     return load_source(name, filename, file)   File "/var/lang/lib/python3.8/imp.py", line 171, in load_source     module = _load(spec)   File "<frozen importlib._bootstrap>", line 702, in _load   File "<frozen importlib._bootstrap>", line 671, in _load_unlocked   File "<frozen importlib._bootstrap_external>", line 843, in exec_module   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed   File "/var/task/lambda_function.py", line 4, in <module>     ec2 = boto3.client('ec2', region_name=ap-southeast-1)START RequestId: ef97215d-066d-402c-adfb-870a0caed854 Version: $LATEST Unknown application error occurred Runtime.Unknown END RequestId: ef97215d-066d-402c-adfb-870a0caed854 REPORT RequestId: ef97215d-066d-402c-adfb-870a0caed854 Duration: 3452.29 ms Billed Duration: 3453 ms Memory Size: 128 MB Max Memory Used: 19 MB

asked 2 years ago1.6K views
1 Answer
2

Hi, you'll find the error message gives you some good pointers:

File "/var/task/lambda_function.py", line 4, in <module>\n ec2 = boto3.client('ec2', region_name=ap-southeast-1)\n" 

You're missing string quotes around the region so Python is looking for a variable 'ap'. Your code should be:

ec2 = boto3.client('ec2', region_name='ap-southeast-1')
EXPERT
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions