skip to Main Content

To save costs I would like to construct / deploy the whole stack (with Aurora and ECS) every working day before business hours and destruct / destroy after these hours.

I found a solution with an EC2 instance, running cron jobs, which executes native cdk commands to deploy and destroy the stack.

Is there a way (on AWS) to trigger these in a serverless kind (without running EC2 instance or something similar that needs to be managed by ourselves) ?

2

Answers


  1. There may be multiple solutions. My approach would be to use a lambda to deploy and destroy assets. The lambda can be invoked using Eventbridge triggers, the eventbridge would be scheduled for start of weekday and end of weekday with inputs to indicate deploy or destroy. I haven’t tried this out, but you would need to package the cdk.json and other cdk code into the lambda. Refer https://github.com/aws/aws-cdk/issues/2637 for a Typescript version

    I created a python version that you can use as a starting point

    import os
    import boto3
    import subprocess
    
    def lambda_handler(event, context):
        """
        This Lambda function can be used to deploy or destroy a CDK stack.
        """
    
        stack_name = event["stack_name"]
        action = event["action"]
    
        if action == "deploy":
            # Execute cdk deploy
            subprocess.run(["cdk", "deploy", stack_name])
        elif action == "destroy":
            # Execute cdk destroy
            subprocess.run(["cdk", "destroy", stack_name])
        else:
            raise ValueError(f"Invalid action: {action}")
    
        return {
            "message": f"Successfully executed `cdk {action}` for stack {stack_name}"
        }
    
    
    Login or Signup to reply.
  2. You can run a CodePipeline job with a scheduled event trigger, as described here.

    For that, you would need to set up a pipeline first, which you can do as described here, or you can utilize the tools provided with CDK, namely CDK Pipelines.

    import * as cdk from 'aws-cdk-lib';
    import { Construct } from 'constructs';
    import { CodePipeline, CodePipelineSource, ShellStep } from 'aws-cdk-lib/pipelines';
    import { MyPipelineAppStage } from './my-pipeline-app-stage';
    
    export class MyPipelineStack extends cdk.Stack {
      constructor(scope: Construct, id: string, props?: cdk.StackProps) {
        super(scope, id, props);
    
        const pipeline = new CodePipeline(this, 'Pipeline', {
          pipelineName: 'MyPipeline',
          synth: new ShellStep('Synth', {
            input: CodePipelineSource.gitHub('OWNER/REPO', 'main'),
            commands: ['npm ci', 'npm run build', 'npx cdk synth']
          })
        });
    
        pipeline.addStage(new MyPipelineAppStage(this, "test", {
          env: { account: "111111111111", region: "eu-west-1" }
        }));
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search