Using AWS's services, specifically EventBridge, you can now detect almost any processes from other services, including ECR and Lambda, and trigger a Lambda function to be invoked upon that detection. Here are some highlights on how to configure this (through the UI and taken from this guide):
- Create a Lambda function that will receive data from EventBridge (you can create an empty fuction and work on it later), the data varies according to the event, for example: a push image to ECR event will have thise format and data (taken from this guide):
{
"account": "123456789012",
"detail": {
"action-type": "PUSH",
"image-digest": "sha256:f98d67af8e53a536502bfc600de3266556b06ed635a32d60aa7a5fe6d7e609d7",
"image-tag": "latest",
"repository-name": "ubuntu",
"result": "SUCCESS"
},
"detail-type": "ECR Image Action",
"id": "4f5ec4d5-4de4-7aad-a046-56d5cfe1df0e",
"region": "us-east-1",
"resources": [],
"source": "aws.ecr",
"time": "2019-08-06T00:58:09Z",
"version": "0"
}
- Go to your Events page in AWS
- Create a rule, for example: detecting a successfully pushed image to ECR
- The event pattern will look somthing like this:
{
"source": ["aws.ecr"],
"detail-type": ["ECR Image Action"],
"detail": {
"action-type": ["PUSH"],
"result": ["SUCCESS"]
}
}
Your Lambda function will need to do the following in general:
- Parse the new image's repository name and image tag from the data coming from the Event
- Use that info to trigger am import job with the snyk-api-import tool
To kick off an import, your Lambda function will need your SNYK_TOKEN
, SNYK_ORG_ID
and ORG_INTEGRATION_ID
(or access to SSM with those parameters)
- Get the "repository-name" and the "image-tag" from the event
- Install or download the latest snyk-api-import tool
- Create a targets file for the tool using your SNYK_ORG_ID, ORG_INTEGRATION_ID as explained here. The import file shsould have the neccessary fileds as needed by our import endpoint, for example - an import file for a new image will look something like this:
{
"targets":[
{
"integrationId":">ORG_INTEGRATION_ID>",
"orgId":"<SNYK_ORG_ID>",
"target":{
"name": "<REPOSITORY_NAME>:<IMAGE_TAG>"
}
}
]
}
- Set your
SNYK_TOKEN
and yourSNYK_LOG_PATH
as an enviroment variables - Kick off an import by running the import command with refernce to the created import file, for example:
DEBUG=snyk* snyk-api-import import --file=path/to/import-targets.json
- Wait for the imported targets log file - "imported-targets.log" to be created
- Check that the log indicates a successful import
- Done!