Reputation: 71
I'm trying to create an AWS lambda script to automate scanning ecr (Elastic Container Registry) images. I have a lambda (python) that loops through my images and gives a boto3 start_image_scan command to initiate a scan. The code works to generate the input to the start_image_scan(). If I use the input to initiate the scan it fails with the error "[ERROR] TypeError: start_image_scan() only accepts keyword arguments." If I hard code the output into the command it works. I've tried a couple of ways to cast the input to a string. I've included them in the code as comments. They failed with the same error message. Any input anyone has would be greatly appreciated.
import json
import boto3
def get_reponames():
client = boto3.client('ecr')
reponames = [repo['repositoryName'] for repo in client.describe_repositories()['repositories']]
return reponames
def get_imageids(prepo):
client = boto3.client('ecr')
imageids = [
{"imageDigest": img['imageDigest'], "imageTag": img.get('imageTag', None)} for img in
client.list_images(repositoryName=prepo, )['imageIds']
]
return imageids
def lambda_handler(event, context):
client = boto3.client('ecr')
output = get_reponames()
for rn in output:
outputii = get_imageids(rn)
for ii in outputii:
if ii['imageTag'].lower() == "latest":
print("scan this image")
scanid = 'repositoryName="'+rn+'", imageId={"imageDigest": "'+ii['imageDigest']+'", "imageTag": "'+ii['imageTag']+'"}'
# scanid = (json.dumps('repositoryName="'+rn+'", imageId={"imageDigest": "'+ii['imageDigest']+'", "imageTag": "'+ii['imageTag']+'"}').replace("\\", ""))[1:-1]
# scanid = (str('repositoryName="'+rn+'", imageId={"imageDigest": "'+ii['imageDigest']+'", "imageTag": "'+ii['imageTag']+'"}')
print(scanid)
# client.start_image_scan(repositoryName="testimage", imageId={'imageDigest': 'sha256:24d7013a7b2805accd177279c6549d08e45059cc5696e32f5c0184f3652daaaa', 'imageTag': '15'})
client.start_image_scan(scanid)
else:
print("DONT SCAN")
return {
'body': json.dumps("hello world")
}
Upvotes: 0
Views: 93
Reputation: 41119
You need to follow the signature of the method, per the documentation.
client.start_image_scan(repositoryName=rn, imageId={'imageDigest': ii['imageDigest']})
You also only need to specify one identifier -- the tag or the digest, not necessarily both.
Since you seem to only want to scan the latest
tag, you could also just do the following:
client.start_image_scan(repositoryName=rn, imageId={'imageTag': 'latest'})
Upvotes: 1