Last active
December 15, 2021 09:39
-
-
Save cm-fujii/def4ae351fee39118040f8eb13743717 to your computer and use it in GitHub Desktop.
AWSCDK-CircleCIDeploySample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as apigateway from '@aws-cdk/aws-apigateway'; | |
import * as lambda from '@aws-cdk/aws-lambda'; | |
import { Duration } from '@aws-cdk/core'; | |
import cdk = require('@aws-cdk/core'); | |
export class AppStack extends cdk.Stack { | |
constructor(scope: cdk.Construct, id: string, systemEnv: string, props?: cdk.StackProps) { | |
super(scope, id, props); | |
// The code that defines your stack goes here | |
// Lambda | |
const sampleLambda = new lambda.Function(this, 'sample-Lambda', { | |
code: lambda.Code.asset('src/lambda'), | |
handler: 'app.handler', | |
runtime: lambda.Runtime.NODEJS_10_X, | |
functionName: `aws-cdk-circleci-deploy-Lambda-${systemEnv}`, | |
timeout: Duration.seconds(3), | |
environment: { | |
SYSTEM_ENV: systemEnv, | |
} | |
}); | |
// API Gateway | |
const api = new apigateway.RestApi(this, 'sample-api', { | |
restApiName: `AWSCDK-CircleCI-Deploy-Api-${systemEnv}` | |
}); | |
const integration = new apigateway.LambdaIntegration(sampleLambda, { | |
proxy: true, | |
}); | |
const messageResource = api.root.addResource('message'); | |
const idResource = messageResource.addResource('{id}'); | |
idResource.addMethod('GET', integration); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function handler(event: any) { | |
const id = event.pathParameters.id; | |
return { | |
statusCode: 200, | |
body: JSON.stringify({ | |
env: process.env.SYSTEM_ENV, | |
message: `your request message id is ${id}` | |
}), | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
import 'source-map-support/register'; | |
import cdk = require('@aws-cdk/core'); | |
import { AppStack } from '../lib/app-stack'; | |
import { CircleCIUserStack } from '../lib/circleci-user-stack'; | |
const targetEnv = process.env.SYSTEM_ENV ? process.env.SYSTEM_ENV : 'dev'; | |
const app = new cdk.App(); | |
new AppStack(app, `AWS-CDK-CircleCI-Deploy-Sample-App-Stack-${targetEnv}`, targetEnv); | |
new CircleCIUserStack(app, `AWS-CDK-CircleCI-Deploy-Sample-User-Stack-${targetEnv}`, targetEnv); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as iam from '@aws-cdk/aws-iam'; | |
import cdk = require('@aws-cdk/core'); | |
import { ManagedPolicy } from '@aws-cdk/aws-iam'; | |
export class CircleCIUserStack extends cdk.Stack { | |
constructor(scope: cdk.Construct, id: string, systemEnv: string, props?: cdk.StackProps) { | |
super(scope, id, props); | |
const user = new iam.User(this, 'CircleCI-User', { | |
userName: `aws-cdk-circleci-deploy-user-${systemEnv}`, | |
managedPolicies: [ | |
ManagedPolicy.fromAwsManagedPolicyName('AWSLambdaFullAccess'), | |
ManagedPolicy.fromAwsManagedPolicyName('AmazonAPIGatewayAdministrator'), | |
ManagedPolicy.fromAwsManagedPolicyName('AWSCloudFormationFullAccess'), | |
ManagedPolicy.fromAwsManagedPolicyName('IAMFullAccess'), | |
] | |
}) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: 2.1 | |
executors: | |
my-executor: | |
# https://circleci.com/docs/2.0/circleci-images/ | |
# https://circleci.com/docs/2.0/docker-image-tags.json | |
docker: | |
- image: circleci/python:3.7.2-node | |
environment: | |
PIPENV_VENV_IN_PROJECT: true | |
working_directory: ~/CircleCIDeploySample | |
commands: | |
restore: | |
steps: | |
- restore_cache: | |
key: CircleCIDeploySample-v1-{{ .Branch }}-{{ checksum "Pipfile.lock" }} | |
- restore_cache: | |
key: CircleCIDeploySample-v1-{{ .Branch }}-{{ checksum "package-lock.json" }} | |
save: | |
steps: | |
- save_cache: | |
paths: | |
- ".venv" | |
key: CircleCIDeploySample-v1-{{ .Branch }}-{{ checksum "Pipfile.lock" }} | |
- save_cache: | |
paths: | |
- "node_modules" | |
key: CircleCIDeploySample-v1-{{ .Branch }}-{{ checksum "package-lock.json" }} | |
jobs: | |
setup: | |
executor: my-executor | |
steps: | |
- checkout | |
- restore | |
- run: | |
name: install | |
command: | | |
sudo pip install pipenv | |
pipenv install | |
npm install | |
- save | |
deploy: | |
executor: my-executor | |
parameters: | |
env: | |
type: enum | |
enum: ["prod", "dev"] | |
steps: | |
- checkout | |
- restore | |
- run: | |
name: deploy | |
command: | | |
source .venv/bin/activate | |
python --version | |
aws --version | |
npm --version | |
node --version | |
npx cdk --version | |
echo << parameters.env >> | |
source ./scripts/set_aws.sh << parameters.env >> | |
export TARGET_APP_STACK_NAME=AWS-CDK-CircleCI-Deploy-Sample-App-Stack-<< parameters.env >> | |
npm run build | |
npx cdk deploy --require-approval never ${TARGET_APP_STACK_NAME} | |
workflows: | |
version: 2.1 | |
release-dev-workflow: | |
jobs: | |
- setup: | |
filters: | |
branches: | |
only: | |
- master | |
- /^issues\/\d+$/ | |
- deploy: | |
env: dev | |
requires: | |
- setup | |
filters: | |
branches: | |
only: | |
- master | |
- /^issues\/\d+$/ | |
release-prod-workflow: | |
jobs: | |
- setup: | |
filters: | |
branches: | |
ignore: /.*/ | |
tags: | |
only: | |
- /^v\d+\.\d+\.\d+$/ | |
- deploy: | |
env: prod | |
requires: | |
- setup | |
filters: | |
branches: | |
ignore: /.*/ | |
tags: | |
only: | |
- /^v\d+\.\d+\.\d+$/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment