-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c972f6
commit 6318f91
Showing
5 changed files
with
80 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,5 +13,6 @@ | |
ComponentParam 'Subnets', type: 'CommaDelimitedList' | ||
end | ||
|
||
LambdaFunctions 'ami_finder_custom_resources' | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import boto3 | ||
from crhelper import CfnResource | ||
import logging | ||
import json | ||
|
||
logger = logging.getLogger(__name__) | ||
# Initialise the helper, all inputs are optional, this example shows the defaults | ||
helper = CfnResource(json_logging=False, log_level='DEBUG', boto_level='CRITICAL') | ||
|
||
@helper.create | ||
def create(event, context): | ||
logger.info(f"Creating resource {event}") | ||
return get_latest_ami(event['ResourceProperties']['Name']) | ||
|
||
@helper.update | ||
def update(event, context): | ||
logger.info(f"Updating resource {event}") | ||
return get_latest_ami(event['ResourceProperties']['Name']) | ||
|
||
@helper.delete | ||
def delete(event, context): | ||
logger.info(f"Deleting resource {event}") | ||
|
||
def get_latest_ami(name): | ||
client = boto3.client('ec2') | ||
|
||
filters = [] | ||
filters.append({'Name': 'name', 'Values': [name]}) | ||
response = client.describe_images(Filters=filters) | ||
|
||
if not response['Images']: | ||
return None | ||
|
||
response['Images'].sort(key=lambda r: r['CreationDate']) | ||
return response['Images'][-1]['ImageId'] | ||
|
||
def handler(event, context): | ||
helper(event, context) | ||
|
||
|
||
'Amzn2_Linux_Jenkins_Agent-*' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
crhelper |