|
1 | 1 | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2 | 2 | // SPDX-License-Identifier: MIT-0
|
3 | 3 |
|
| 4 | +import { BuildSpec } from "aws-cdk-lib/aws-codebuild"; |
| 5 | + |
| 6 | +const defaultDestroyBuildSpec = ` |
| 7 | +version: 0.2 |
| 8 | +env: |
| 9 | + variables: |
| 10 | + CFN_RESPONSE_URL: CFN_RESPONSE_URL_NOT_SET |
| 11 | + CFN_STACK_ID: CFN_STACK_ID_NOT_SET |
| 12 | + CFN_REQUEST_ID: CFN_REQUEST_ID_NOT_SET |
| 13 | + CFN_LOGICAL_RESOURCE_ID: CFN_LOGICAL_RESOURCE_ID_NOT_SET |
| 14 | +phases: |
| 15 | + pre_build: |
| 16 | + on-failure: ABORT |
| 17 | + commands: |
| 18 | + - echo "Default destroy buildspec" |
| 19 | + - cd $CODEBUILD_SRC_DIR/$CDK_APP_LOCATION |
| 20 | + - npm install -g aws-cdk && sudo apt-get install python3 && python -m |
| 21 | + ensurepip --upgrade && python -m pip install --upgrade pip && python -m |
| 22 | + pip install -r requirements.txt |
| 23 | + - \"export AWS_ACCOUNT_ID=$(echo $CODEBUILD_BUILD_ARN | cut -d: -f5)\" |
| 24 | + - 'echo \"AWS_ACCOUNT_ID: $AWS_ACCOUNT_ID\"' |
| 25 | + - cdk bootstrap aws://$AWS_ACCOUNT_ID/$AWS_REGION |
| 26 | + build: |
| 27 | + on-failure: ABORT |
| 28 | + commands: |
| 29 | + - \"export AWS_ACCOUNT_ID=$(echo $CODEBUILD_BUILD_ARN | cut -d: -f5)\" |
| 30 | + - 'echo \"AWS_ACCOUNT_ID: $AWS_ACCOUNT_ID\"' |
| 31 | + - cdk destroy --force --all --require-approval never |
| 32 | +` |
| 33 | + |
| 34 | +const defaultDeployBuildSpec = ` |
| 35 | +version: 0.2 |
| 36 | +env: |
| 37 | + variables: |
| 38 | + CFN_RESPONSE_URL: CFN_RESPONSE_URL_NOT_SET |
| 39 | + CFN_STACK_ID: CFN_STACK_ID_NOT_SET |
| 40 | + CFN_REQUEST_ID: CFN_REQUEST_ID_NOT_SET |
| 41 | + CFN_LOGICAL_RESOURCE_ID: CFN_LOGICAL_RESOURCE_ID_NOT_SET |
| 42 | + PARAMETERS: PARAMETERS_NOT_SET |
| 43 | + STACKNAME: STACKNAME_NOT_SET |
| 44 | +phases: |
| 45 | + pre_build: |
| 46 | + on-failure: ABORT |
| 47 | + commands: |
| 48 | + - echo "Default deploy buildspec" |
| 49 | + - cd $CODEBUILD_SRC_DIR/$CDK_APP_LOCATION |
| 50 | + - npm install -g aws-cdk && sudo apt-get install python3 && python -m |
| 51 | + ensurepip --upgrade && python -m pip install --upgrade pip && python -m |
| 52 | + pip install -r requirements.txt |
| 53 | + - \"export AWS_ACCOUNT_ID=$(echo $CODEBUILD_BUILD_ARN | cut -d: -f5)\" |
| 54 | + - 'echo \"AWS_ACCOUNT_ID: $AWS_ACCOUNT_ID\"' |
| 55 | + - cdk bootstrap aws://$AWS_ACCOUNT_ID/$AWS_REGION |
| 56 | + build: |
| 57 | + on-failure: ABORT |
| 58 | + commands: |
| 59 | + - \"export AWS_ACCOUNT_ID=$(echo $CODEBUILD_BUILD_ARN | cut -d: -f5)\" |
| 60 | + - 'echo \"AWS_ACCOUNT_ID: $AWS_ACCOUNT_ID\"' |
| 61 | + - cdk deploy $STACKNAME $PARAMETERS --require-approval=never |
| 62 | +` |
| 63 | + |
4 | 64 | // workaround to get a Lambda function with inline code and packaged into the ARA library
|
5 | 65 | // We need inline code to ensure it's deployable via a CloudFormation template
|
6 | 66 | // TODO modify the PreBundledFunction to allow for inline Lambda in addtion to asset based Lambda
|
7 |
| -export const startBuild = "const respond = async function(event, context, responseStatus, responseData, physicalResourceId, noEcho) {\n return new Promise((resolve, reject) => {\n var responseBody = JSON.stringify({\n Status: responseStatus,\n Reason: \"See the details in CloudWatch Log Stream: \" + context.logGroupName + \" \" + context.logStreamName,\n PhysicalResourceId: physicalResourceId || context.logStreamName,\n StackId: event.StackId,\n RequestId: event.RequestId,\n LogicalResourceId: event.LogicalResourceId,\n NoEcho: noEcho || false,\n Data: responseData\n });\n \n console.log(\"Response body:\", responseBody);\n \n var https = require(\"https\");\n var url = require(\"url\");\n \n var parsedUrl = url.parse(event.ResponseURL);\n var options = {\n hostname: parsedUrl.hostname,\n port: 443,\n path: parsedUrl.path,\n method: \"PUT\",\n headers: {\n \"content-type\": \"\",\n \"content-length\": responseBody.length\n }\n };\n \n var request = https.request(options, function(response) {\n console.log(\"Status code: \" + response.statusCode);\n console.log(\"Status message: \" + response.statusMessage);\n resolve();\n });\n \n request.on(\"error\", function(error) {\n console.log(\"respond(..) failed executing https.request(..): \" + error);\n resolve();\n });\n \n request.write(responseBody);\n request.end();\n });\n};\n\nconst AWS = require('aws-sdk');\n\nexports.handler = async function (event, context) {\n console.log(JSON.stringify(event, null, 4));\n try {\n const projectName = event.ResourceProperties.ProjectName;\n const codebuild = new AWS.CodeBuild();\n \n console.log(`Starting new build of project ${projectName}`);\n \n const { build } = await codebuild.startBuild({\n projectName,\n // Pass CFN related parameters through the build for extraction by the\n // completion handler.\n buildspecOverride: event.RequestType === 'Delete' ? \n `\nversion: 0.2\nenv:\n variables:\n CFN_RESPONSE_URL: CFN_RESPONSE_URL_NOT_SET\n CFN_STACK_ID: CFN_STACK_ID_NOT_SET\n CFN_REQUEST_ID: CFN_REQUEST_ID_NOT_SET\n CFN_LOGICAL_RESOURCE_ID: CFN_LOGICAL_RESOURCE_ID_NOT_SET\nphases:\n pre_build:\n on-failure: ABORT\n commands:\n - cd $CODEBUILD_SRC_DIR/$CDK_APP_LOCATION\n - npm install -g aws-cdk && sudo apt-get install python3 && python -m\n ensurepip --upgrade && python -m pip install --upgrade pip && python -m\n pip install -r requirements.txt\n - \"export AWS_ACCOUNT_ID=$(echo $CODEBUILD_BUILD_ARN | cut -d: -f5)\"\n - 'echo \"AWS_ACCOUNT_ID: $AWS_ACCOUNT_ID\"'\n - cdk bootstrap aws://$AWS_ACCOUNT_ID/$AWS_REGION\n build:\n on-failure: ABORT\n commands:\n - \"export AWS_ACCOUNT_ID=$(echo $CODEBUILD_BUILD_ARN | cut -d: -f5)\"\n - 'echo \"AWS_ACCOUNT_ID: $AWS_ACCOUNT_ID\"'\n - cdk destroy --force --all --require-approval never\n `\n :\n `\nversion: 0.2\nenv:\n variables:\n CFN_RESPONSE_URL: CFN_RESPONSE_URL_NOT_SET\n CFN_STACK_ID: CFN_STACK_ID_NOT_SET\n CFN_REQUEST_ID: CFN_REQUEST_ID_NOT_SET\n CFN_LOGICAL_RESOURCE_ID: CFN_LOGICAL_RESOURCE_ID_NOT_SET\n PARAMETERS: PARAMETERS_NOT_SET\n STACKNAME: STACKNAME_NOT_SET\nphases:\n pre_build:\n on-failure: ABORT\n commands:\n - cd $CODEBUILD_SRC_DIR/$CDK_APP_LOCATION\n - npm install -g aws-cdk && sudo apt-get install python3 && python -m\n ensurepip --upgrade && python -m pip install --upgrade pip && python -m\n pip install -r requirements.txt\n - \"export AWS_ACCOUNT_ID=$(echo $CODEBUILD_BUILD_ARN | cut -d: -f5)\"\n - 'echo \"AWS_ACCOUNT_ID: $AWS_ACCOUNT_ID\"'\n - cdk bootstrap aws://$AWS_ACCOUNT_ID/$AWS_REGION\n build:\n on-failure: ABORT\n commands:\n - \"export AWS_ACCOUNT_ID=$(echo $CODEBUILD_BUILD_ARN | cut -d: -f5)\"\n - 'echo \"AWS_ACCOUNT_ID: $AWS_ACCOUNT_ID\"'\n - cdk deploy $STACKNAME $PARAMETERS --require-approval=never\n `,\n environmentVariablesOverride: [\n {\n name: 'CFN_RESPONSE_URL',\n value: event.ResponseURL\n },\n {\n name: 'CFN_STACK_ID',\n value: event.StackId\n },\n {\n name: 'CFN_REQUEST_ID',\n value: event.RequestId\n },\n {\n name: 'CFN_LOGICAL_RESOURCE_ID',\n value: event.LogicalResourceId\n },\n {\n name: 'BUILD_ROLE_ARN',\n value: event.ResourceProperties.BuildRoleArn\n }\n ]\n }).promise();\n console.log(`Build id ${build.id} started - resource completion handled by EventBridge`);\n } catch(error) {\n console.error(error);\n await respond(event, context, 'FAILED', { Error: error });\n }\n};" |
| 67 | +export const startBuild = (deployBuildSpec?: BuildSpec, destroyBuildSpec?: BuildSpec) => { return ` |
| 68 | +const respond = async function(event, context, responseStatus, responseData, physicalResourceId, noEcho) { |
| 69 | + return new Promise((resolve, reject) => { |
| 70 | + var responseBody = JSON.stringify({ |
| 71 | + Status: responseStatus, |
| 72 | + Reason: \"See the details in CloudWatch Log Stream: \" + context.logGroupName + \" \" + context.logStreamName, |
| 73 | + PhysicalResourceId: physicalResourceId || context.logStreamName, |
| 74 | + StackId: event.StackId, |
| 75 | + RequestId: event.RequestId, |
| 76 | + LogicalResourceId: event.LogicalResourceId, |
| 77 | + NoEcho: noEcho || false, |
| 78 | + Data: responseData |
| 79 | + }); |
| 80 | + |
| 81 | + console.log(\"Response body:\", responseBody); |
| 82 | + |
| 83 | + var https = require(\"https\"); |
| 84 | + var url = require(\"url\"); |
| 85 | + |
| 86 | + var parsedUrl = url.parse(event.ResponseURL); |
| 87 | + var options = { |
| 88 | + hostname: parsedUrl.hostname, |
| 89 | + port: 443, |
| 90 | + path: parsedUrl.path, |
| 91 | + method: \"PUT\", |
| 92 | + headers: { |
| 93 | + \"content-type\": \"\", |
| 94 | + \"content-length\": responseBody.length |
| 95 | + } |
| 96 | + }; |
| 97 | + |
| 98 | + var request = https.request(options, function(response) { |
| 99 | + console.log(\"Status code: \" + response.statusCode); |
| 100 | + console.log(\"Status message: \" + response.statusMessage); |
| 101 | + resolve(); |
| 102 | + }); |
| 103 | + |
| 104 | + request.on(\"error\", function(error) { |
| 105 | + console.log(\"respond(..) failed executing https.request(..): \" + error); |
| 106 | + resolve(); |
| 107 | + }); |
| 108 | + |
| 109 | + request.write(responseBody); |
| 110 | + request.end(); |
| 111 | + }); |
| 112 | +}; |
| 113 | +
|
| 114 | +const AWS = require('aws-sdk'); |
| 115 | +
|
| 116 | +exports.handler = async function (event, context) { |
| 117 | + console.log(JSON.stringify(event, null, 4)); |
| 118 | + try { |
| 119 | + const projectName = event.ResourceProperties.ProjectName; |
| 120 | + const codebuild = new AWS.CodeBuild(); |
| 121 | + |
| 122 | + console.log(\`Starting new build of project \${projectName}\`); |
| 123 | + |
| 124 | + const { build } = await codebuild.startBuild({ |
| 125 | + projectName, |
| 126 | + // Pass CFN related parameters through the build for extraction by the |
| 127 | + // completion handler. |
| 128 | + buildspecOverride: event.RequestType === 'Delete' ? \`${destroyBuildSpec ? `${destroyBuildSpec.toBuildSpec()}` : defaultDestroyBuildSpec}\` : \`${deployBuildSpec ? `${deployBuildSpec.toBuildSpec()}` : defaultDeployBuildSpec}\`, |
| 129 | + environmentVariablesOverride: [ |
| 130 | + { |
| 131 | + name: 'CFN_RESPONSE_URL', |
| 132 | + value: event.ResponseURL |
| 133 | + }, |
| 134 | + { |
| 135 | + name: 'CFN_STACK_ID', |
| 136 | + value: event.StackId |
| 137 | + }, |
| 138 | + { |
| 139 | + name: 'CFN_REQUEST_ID', |
| 140 | + value: event.RequestId |
| 141 | + }, |
| 142 | + { |
| 143 | + name: 'CFN_LOGICAL_RESOURCE_ID', |
| 144 | + value: event.LogicalResourceId |
| 145 | + }, |
| 146 | + { |
| 147 | + name: 'BUILD_ROLE_ARN', |
| 148 | + value: event.ResourceProperties.BuildRoleArn |
| 149 | + } |
| 150 | + ] |
| 151 | + }).promise(); |
| 152 | + console.log(\`Build id \${build.id} started - resource completion handled by EventBridge\`); |
| 153 | + } catch(error) { |
| 154 | + console.error(error); |
| 155 | + await respond(event, context, 'FAILED', { Error: error }); |
| 156 | + } |
| 157 | +}; |
| 158 | +`}; |
8 | 159 |
|
9 | 160 | export const reportBuild = `
|
10 | 161 | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
0 commit comments