Skip to content

Commit

Permalink
Merge pull request #8 from umutykaya/fix/rds_instance_type
Browse files Browse the repository at this point in the history
Fix/rds instance type
  • Loading branch information
umutykaya authored May 29, 2021
2 parents a53fc4a + becb483 commit dbde05b
Show file tree
Hide file tree
Showing 5 changed files with 589 additions and 225 deletions.
58 changes: 42 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,36 +52,62 @@ You can see the following file structure while you clone the project.
- [ ] Use your own AWS CLI Credentials
- [ ] Create Secret Manager with Github token
- [ ] Add Environment variables
- [ ] AWS CDK Bootstrap & Sync
- [ ] AWS CDK Bootstrap, Sync &

Suppose that you've already fork or clone the repository. Please find the main class `CDKSpringPipeline` and change the attributes and fill with your own credentials.




### Github Credentials

You need to create following
- Personal access token: https://github.com/settings/tokens/new
- Token: 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

`CDKSpringPipeline` class`cdk-spring-pipeline-stack.ts`. Dependant on your secret name oauth value is mutable and you can change it in below.
```typescript
new codepipelineactions.GitHubSourceAction({
actionName: 'GitHub_Source',
owner: '<nickname>',
repo: '<repo_name>',
branch: '<branch>',
oauthToken: cdk.SecretValue.secretsManager("<secret_name>"),
output: sourceOutput
})
```
Inside of the `gh_token.json` you should pass value as plain text format. ex: `ghp_1234bkLW89212`. [Here](https://github.com/umutykaya/cdk-spring-pipeline/blob/master/assets/docs/github_token.md), it explains to create Github personal access token. Then, create a Secret Manager resource called `pipeline/secret`.
`CDKSpringPipeline` class`cdk-spring-pipeline-stack.ts`. Dependant on your secret name oauth value is mutable and you can change it. Inside of the `gh_token.json` you should pass value as plain text format. ex: `ghp_1234bkLW89212`. [Here](https://github.com/umutykaya/cdk-spring-pipeline/blob/master/assets/docs/github_token.md), it explains to create Github personal access token. Then, create a Secret Manager resource called `pipeline/secret`.

```bash
aws secretsmanager create-secret --name pipeline/spring-boot-react \
--description "spring-boot-react" \
--secret-string file://gh_token.json
```
### Environment Variables


```bash
export myIP='0.0.0.0/0'
export domainName='subdomain.example.com'
export certArn='arn:aws:acm:<region>:<account_id>:certificate/<certificate_id>'
export hostedZoneId='zone_id'
export rdsSecretName='pipeline/rds'
export owner='repo_owner'
export repo='repo_name'
export branch='master'
export ghbSecretName='pipeline/secret'
export clusterName='spring-cluster'
export serviceName='spring-service'
```

## Workflow
To run the project, use below scripts iteratively in your terminal.

```bash
export CDK_NEW_BOOTSTRAP=0
export CDK_DEFAULT_ACCOUNT=<account_id>
export CDK_DEFAULT_REGION=<region>
cdk bootstrap --show-template > bootstrap-template.yaml
cdk bootstrap aws://$CDK_DEFAULT_ACCOUNT/$CDK_DEFAULT_REGION --cloudformation-execution-policies arn:aws:iam::aws:policy/AdministratorAccess --template bootstrap-template.yaml
```

Install project specific dependencies.

```bash
npm install @types/node
npm install
npm run build
```
You can check diff of changes, synthesize your application stack and deploy.

```bash
cdk diff
cdk synth
cdk deploy
```
6 changes: 1 addition & 5 deletions bin/cdk-spring-pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import 'source-map-support/register';
import cdk = require('@aws-cdk/core');
import { CDKSpringPipeline } from '../lib/cdk-spring-pipeline-stack';
// import { RdsBastionStack } from '../lib/rds-postgres-stack';

const app = new cdk.App();

Expand All @@ -11,7 +10,4 @@ const env = {
account: app.node.tryGetContext('account') || process.env.CDK_INTEG_ACCOUNT || process.env.CDK_DEFAULT_ACCOUNT
};

new CDKSpringPipeline(app, 'CDKSpringPipeline');
// new RdsBastionStack(app, 'RdsBastionStack');


new CDKSpringPipeline(app, 'CDKSpringPipeline');
43 changes: 26 additions & 17 deletions lib/cdk-spring-pipeline-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@ import elbv2 = require('@aws-cdk/aws-elasticloadbalancingv2');
import acm = require('@aws-cdk/aws-certificatemanager');
import route53 = require('@aws-cdk/aws-route53');

const myIP = '0.0.0.0/0'; // IP address from which you want to connect to RDS
const rdsSecretName = 'pipeline/rds';
const ghbSecretName = 'pipeline/secret';
const myIP = process.env.myIP || '0.0.0.0/0';
const domainName = process.env.domainName || 'subdomain.example.com';
const certArn = process.env.certArn || 'arn:aws:acm:<region>:<account_id>:certificate/<certificate_id>';
const hostedZoneId = process.env.hostedZoneId || 'hosted_zone_id';
const rdsSecretName = process.env.rdsSecretName || 'pipeline/rds';
const owner = process.env.owner || 'umutykaya';
const repo = process.env.repo || 'spring-boot-react';
const branch = process.env.branch || 'master';
const ghbSecretName = process.env.ghbSecretName || 'pipeline/secret';
const clusterName = process.env.clusterName || 'spring-cluster';
const serviceName = process.env.serviceName || 'spring-service';

export class CDKSpringPipeline extends cdk.Stack {
projectName: string = 'cdk-spring-pipeline';
Expand Down Expand Up @@ -62,6 +70,7 @@ export class CDKSpringPipeline extends cdk.Stack {
deletionProtection: false,
credentials: rds.Credentials.fromGeneratedSecret('postgres',{secretName: rdsSecretName}), // Creates an admin user of postgres with a generated password
publiclyAccessible: true,
instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO),
vpcSubnets: {
subnetType: ec2.SubnetType.PUBLIC
}
Expand All @@ -86,8 +95,7 @@ export class CDKSpringPipeline extends cdk.Stack {
* Route53 and ACM constructs
*/

const arn = 'arn:aws:acm:eu-west-1:223705206905:certificate/c3ec789f-ef9a-4533-ad92-b94dba2a4db8';
const certificate = acm.Certificate.fromCertificateArn(this, 'certificate', arn);
const certificate = acm.Certificate.fromCertificateArn(this, 'certificate', certArn);

const loadBalancer = new elbv2.ApplicationLoadBalancer(this, 'LB', {
loadBalancerName: `${this.projectName}-lb`,
Expand All @@ -96,12 +104,12 @@ export class CDKSpringPipeline extends cdk.Stack {
});

const hostedZone = route53.HostedZone.fromHostedZoneAttributes(this, 'hostedZone', {
hostedZoneId: 'Z0309870FZYTOAVNETRD',
zoneName: 'umutykaya.com'
hostedZoneId,
zoneName: 'commencis-cloud.com'
})

const cluster = new ecs.Cluster(this, "cluster", {
clusterName: 'spring-boot-service',
clusterName,
vpc: vpc,
containerInsights: true,
});
Expand Down Expand Up @@ -153,13 +161,13 @@ export class CDKSpringPipeline extends cdk.Stack {


const fargateService = new ecs_patterns.ApplicationLoadBalancedFargateService(this, "fargateService", {
serviceName: 'spring-boot-service',
serviceName,
loadBalancer: loadBalancer,
cluster: cluster,
securityGroups: [serviceToDBGroup],
taskDefinition: taskDef,
domainZone: hostedZone,
domainName: 'spring.umutykaya.com',
domainName,
// redirectHTTP: true,
minHealthyPercent: 100,
// certificate: certificate,
Expand All @@ -183,13 +191,13 @@ export class CDKSpringPipeline extends cdk.Stack {
// ECR - repo
const ecrRepo = new ecr.Repository(this, 'EcrRepo', {
imageScanOnPush: true,
repositoryName: 'spring-boot-service',
repositoryName: 'spring-boot-react',
removalPolicy: cdk.RemovalPolicy.DESTROY
});

const gitHubSource = codebuild.Source.gitHub({
owner: 'umutykaya',
repo: 'spring-boot-service',
owner,
repo,
});


Expand Down Expand Up @@ -280,9 +288,9 @@ export class CDKSpringPipeline extends cdk.Stack {
actions: [
new codepipelineactions.GitHubSourceAction({
actionName: 'GitHub_Source',
owner: 'umutykaya',
repo: 'spring-boot-react',
branch: 'develop',
owner,
repo,
branch,
oauthToken: cdk.SecretValue.secretsManager(ghbSecretName),
output: sourceOutput
}),
Expand Down Expand Up @@ -326,9 +334,10 @@ export class CDKSpringPipeline extends cdk.Stack {
//OUTPUT
new cdk.CfnOutput(this, "publicDNS", { value: bastion.instance.instancePublicDnsName });
new cdk.CfnOutput(this, "instanceID", { value: bastion.instanceId });
new cdk.CfnOutput(this, "rdsSecretName", { value: rdsSecretName });
new cdk.CfnOutput(this, 'LoadBalancerDNS', { value: fargateService.loadBalancer.loadBalancerDnsName });
new cdk.CfnOutput(this, "rdsSecretName", { value: rdsSecretName });
new cdk.CfnOutput(this, 'RDSEndpoint', { value: rdsInstance.dbInstanceEndpointAddress });
new cdk.CfnOutput(this, 'RDSIdentifier', { value: rdsInstance.instanceIdentifier });

}
}
Loading

0 comments on commit dbde05b

Please sign in to comment.