This is my first lambda function but the python code should be correct yet I am getting an error.
Python code:
import json
import boto3
def get_volume_id_from_arn(volume_arn):
#Split the ARN using the colon (':') separator
arn_parts = volume_arn.split(':')
#The Volume ID is the last part of the ARN after the 'volume/' prefix
volume_id = arn_parts[-1].split('/')[-1]
return volume_id
def lambda_handler(event, context):
print(event)
#Grab the event and look for resources
#Grab the first resource line
volume_arn = event['resources'][0]
#Call get_volume_id_from_arn and pass it the arn for conversion
volume_id = get_volume_id_from_arn(volume_arn)
#Create an ec2 client for boto3
ec2_client = boto3.client('ec2')
#Use boto3 to call the modify_volume function to convert the volume to gp3
responce = client.modify_volume(
VolumeId=volume_id,
VolumeType='gp3'
)
When I deploy and test I get this result:
Response
{
"errorMessage": "'resources'",
"errorType": "KeyError",
"requestId": "3d116529-e903-4b49-9014-c00a5d23a7ca",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 16, in lambda_handler\n volume_arn = event['resources'][0]\n"
]
}
Function Logs
START RequestId: 3d116529-e903-4b49-9014-c00a5d23a7ca Version: $LATEST
{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
[ERROR] KeyError: 'resources'
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 16, in lambda_handler
volume_arn = event['resources'][0]END RequestId: 3d116529-e903-4b49-9014-c00a5d23a7ca
REPORT RequestId: 3d116529-e903-4b49-9014-c00a5d23a7ca Duration: 1.55 ms Billed Duration: 2 ms Memory Size: 128 MB Max Memory Used: 60 MB
Request ID
3d116529-e903-4b49-9014-c00a5d23a7ca
Why does it not like resources as a key name?