有没有办法在一定时间后自动删除 aws codepipeline?
Is there a way to automatically delete an aws codepipeline after a certain time?
我对 AWS 服务还很陌生,所以请多多包涵。我目前正在从事一个项目,我试图在 24 小时后关闭实时环境(在 aws codepipeline 上)。似乎没有在创建过程中对管道设置时间限制的选项。鉴于此,有没有做这样的事情的好方法?我正在考虑创建某种可以从外部触发的 lambda 函数来删除某个管道(因为它看起来像是 possible to run aws cli codes with lambda)。但同样,我对此还是很陌生,不确定是否可行。
所以,我想出了一个办法。最简单的方法似乎是创建一个 Cloudwatch 规则,该规则计划在每小时或每天调用 lambda 函数。下面是我创建的 boto3 lambda 函数,用于对 Codestar 中超过 24 小时的管道进行排序并将其删除。
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)
我对 AWS 服务还很陌生,所以请多多包涵。我目前正在从事一个项目,我试图在 24 小时后关闭实时环境(在 aws codepipeline 上)。似乎没有在创建过程中对管道设置时间限制的选项。鉴于此,有没有做这样的事情的好方法?我正在考虑创建某种可以从外部触发的 lambda 函数来删除某个管道(因为它看起来像是 possible to run aws cli codes with lambda)。但同样,我对此还是很陌生,不确定是否可行。
所以,我想出了一个办法。最简单的方法似乎是创建一个 Cloudwatch 规则,该规则计划在每小时或每天调用 lambda 函数。下面是我创建的 boto3 lambda 函数,用于对 Codestar 中超过 24 小时的管道进行排序并将其删除。
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)