有没有办法在一定时间后自动删除AWS Codepipeline?

时间:2018-06-19 18:27:11

标签: amazon-web-services aws-lambda aws-codepipeline

我对AWS服务非常陌生,因此请在这里与我联系。我目前正在一个项目中,尝试在24小时后关闭实时环境(在AWS Codepipeline上)。在创建过程中似乎没有设置时间限制的选项。鉴于此,有没有办法做类似的事情?我正在考虑创建某种lambda函数,该函数可以从外部触发以删除某个管道(因为它看起来像是possible to run aws cli codes with lambda)。但是再次,我对此还是很陌生,不确定是否有可能。

1 个答案:

答案 0 :(得分:0)

所以,我想出了一条出路。最简单的方法似乎是创建一个Cloudwatch规则,该规则计划为每小时或每天调用一次lambda函数的时间。下面是我创建的boto3 lambda函数,用于对早于24小时的管道进行Codestar排序并删除它们。

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)