Reputation: 33
Trying to run a Lambda function to invoke SSM and define an EC2 tag to push the same on multiple instances with the below script. Getting the below error when trying to execute. I am just started learning to write a script and using aws lambda first time. Please help me to fix.
import boto3
ssm = boto3.client('ssm')
ec2 = boto3.resource('ec2')
def lambda_handler(event, context):
filters = (
Name = 'tag:Product',
Values = ['Essay']
)
instances = ('filters')
response = ssm.send_command(
InstanceIds=instances,
DocumentName='xxxxxxxxxxxxx',
DocumentVersion='$DEFAULT',
DocumentHash='916fdxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxdcdbe7940',
DocumentHashType='Sha256'
)
print(response)
Error :
Response:
{
"errorMessage": "Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 7)",
"errorType": "Runtime.UserCodeSyntaxError",
"stackTrace": [
" File \"/var/task/lambda_function.py\" Line 7\n Name = 'tag:Product',\n"
]
}
Request ID:
"8cb4cd39-b744-41da-befb-5f60b6e49fa4"
Function logs:
START RequestId: 8cb4cd39-b744-41da-befb-5f60b6e49fa4 Version: $LATEST
[ERROR] Runtime.UserCodeSyntaxError: Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 7)
Traceback (most recent call last):
File "/var/task/lambda_function.py" Line 7
Name = 'tag:Product',END RequestId: 8cb4cd39-b744-41da-befb-5f60b6e49fa4
REPORT RequestId: 8cb4cd39-b744-41da-befb-5f60b6e49fa4
Upvotes: 2
Views: 1096
Reputation: 238209
There are several issues:
Wrong indentation.
There is no such thing in python as
filters = (
Name = 'tag:Product',
Values = ['Essay']
)
maybe you meant dictionary?:
filters = {
'Name':'tag:Product',
'Values': ['Essay']
}
InstanceIds=instances
should be a list of strings, not a literal string of 'filters'
.
The closes to fixing the code is the following:
import boto3
ssm = boto3.client('ssm')
ec2 = boto3.resource('ec2')
def lambda_handler(event, context):
filters = [{
'Name':'tag:Product',
'Values': ['Essay']
}]
instances = [instance.id for instance in ec2.instances.filter(Filters = filters)]
response = ssm.send_command(
InstanceIds=instances,
DocumentName='xxxxxxxxxxxxx',
DocumentVersion='$DEFAULT',
DocumentHash='916fdxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxdcdbe7940',
DocumentHashType='Sha256'
)
print(response)
Upvotes: 2