Reputation: 31
I am pretty new to the AWS services so please bear with me here. I am currently working on a project where I am trying to shut down the live environments (on aws codepipeline) after 24 hours. It seems like there isn’t an option to set a time limit on a pipeline during creation. Given that, is there a good way to do something like this? I was thinking of creating some sort of lambda function that can be triggered externally to delete a certain pipeline (since it seems like it is possible to run aws cli codes with lambda). But again, I am still very new to this and am not sure if it is possible.
Upvotes: 1
Views: 1225
Reputation: 31
So, I figured a way out myself. The easiest way seems to be creating a Cloudwatch rule that is scheduled for every hour or day that invokes a lambda function. Below is the boto3 lambda function I created to sort through the Codestar for pipelines that are older than 24 hours and deleting them.
import boto3
from datetime import datetime, timedelta, timezone
# 24 hours limit from lambda invocation
timeLimit = (datetime.now(timezone.utc) - timedelta(hours=24))
# Create an Codestar client
client = boto3.client('codestar', region_name='us-east-1')
# begins lambda function
def lambda_handler(event, context):
# Call Codestar to list current projects
response = client.list_projects()
# Get a list of all project ids from the response
projectIds = [projects['projectId'] for projects in response['projects']]
# Iterate through projects
for projectId in projectIds:
# Gets time stamp for each project
description = client.describe_project(id=projectId)
# If a project is older than 24 hours, then the project is deleted
if description['createdTimeStamp'] < timeLimit:
response = client.delete_project(id=projectId, deleteStack=True)
Upvotes: 1