Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -509,4 +509,5 @@ cdk.context.json
.cdk.staging
cdk.out

.vscode
.vscode
.package-lock.json
4 changes: 3 additions & 1 deletion node/s3-sqs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ cdk.out
*.snk
*.js
*.d.ts
*.swp
*.swp
package-lock.json
*.js.map
3 changes: 2 additions & 1 deletion node/s3-sqs/bin/s3-sqs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const app = new App();

new PipelineStack(app, 'PipelineStack', {
env : {
region : 'us-east-1'
region : 'us-east-1',
account: process.env.CDK_DEFAULT_ACCOUNT,
}
});
6 changes: 6 additions & 0 deletions node/s3-sqs/function/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Context, SQSEvent } from 'aws-lambda';

export const handler = async (event: SQSEvent, context: Context): Promise<any> => {
console.log("EVENT=%s",JSON.stringify(event), context);
return "test response";
}
80 changes: 40 additions & 40 deletions node/s3-sqs/lib/pipeline-stack.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as codepipeline from '@aws-cdk/aws-codepipeline';
import * as codepipeline_actions from '@aws-cdk/aws-codepipeline-actions';
import { Construct, SecretValue, Stack, StackProps } from '@aws-cdk/core';
import { CdkPipeline, ShellScriptAction, SimpleSynthAction } from "@aws-cdk/pipelines";
import { PipelinesStage } from './pipeline-stage';
import { PolicyStatement } from "@aws-cdk/aws-iam"
import { CdkPipeline, SimpleSynthAction } from "@aws-cdk/pipelines";
// import { PipelinesStage } from './pipeline-stage';
// import { PolicyStatement } from "@aws-cdk/aws-iam"

/**
* The stack that defines the application pipeline
Expand All @@ -22,7 +22,7 @@ export class PipelineStack extends Stack {
sourceAction: new codepipeline_actions.GitHubSourceAction({
actionName: 'GitHub',
output: sourceArtifact,
branch: 'master',
branch: 'sqs_lambda_trigger',
oauthToken: SecretValue.secretsManager('github-token-new'),
owner: 'OperationalFallacy',
repo: 'aws-cdk-examples',
Expand All @@ -37,46 +37,46 @@ export class PipelineStack extends Stack {
buildCommand: 'npm run build'
}),
});

// This is where we add the application stages - it should be branch-based perhaps
const devstage = new PipelinesStage(this, 'DeployDev', {
env: { region: 'us-east-1' }
},
{
stacksettings: {
environment: 'dev'
}
});
console.log(pipeline)
// // This is where we add the application stages - it should be branch-based perhaps
// const devstage = new PipelinesStage(this, 'DeployDev', {
// env: { region: 'us-east-1' }
// },
// {
// stacksettings: {
// environment: 'dev'
// }
// });

const deploydev = pipeline.addApplicationStage(devstage);
// const deploydev = pipeline.addApplicationStage(devstage);

const policy = new PolicyStatement({
actions: [ "s3:ListAllMyBuckets" ],
resources: [ "arn:aws:s3:::*" ]
});
// const policy = new PolicyStatement({
// actions: [ "s3:ListAllMyBuckets" ],
// resources: [ "arn:aws:s3:::*" ]
// });

deploydev.addActions(new ShellScriptAction({
actionName: 'TestInfra',
rolePolicyStatements: [ policy ],
useOutputs: {
// Get the stack Output from the Stage and make it available in
// the shell script as $BucketName.
BucketName: pipeline.stackOutput(devstage.BucketName),
},
commands: [
// Use 'curl' to GET the given URL and fail if it returns an error
'aws s3 ls | grep $BucketName',
],
}));
// deploydev.addActions(new ShellScriptAction({
// actionName: 'TestInfra',
// rolePolicyStatements: [ policy ],
// // useOutputs: {
// // // Get the stack Output from the Stage and make it available in
// // // the shell script as $BucketName.
// // BucketName: pipeline.stackOutput(devstage.BucketName),
// // },
// commands: [
// // Use 'curl' to GET the given URL and fail if it returns an error
// 'aws s3 ls',
// ],
// }));

pipeline.addApplicationStage(new PipelinesStage(this, 'DeployProd', {
env: { region: 'us-east-1' }
},
{
stacksettings: {
environment: 'prod'
}
}));
// pipeline.addApplicationStage(new PipelinesStage(this, 'DeployProd', {
// env: { region: 'us-east-1' }
// },
// {
// stacksettings: {
// environment: 'prod'
// }
// }));

}
}
Expand Down
28 changes: 26 additions & 2 deletions node/s3-sqs/lib/s3-sqs-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { Queue } from '@aws-cdk/aws-sqs';
import { SqsDestination} from '@aws-cdk/aws-s3-notifications';
import { ServicePrincipal, Role, PolicyStatement } from '@aws-cdk/aws-iam';
import { Construct, StackProps } from '@aws-cdk/core';
import { SqsEventSource } from '@aws-cdk/aws-lambda-event-sources';
import { Function, Runtime, Code } from '@aws-cdk/aws-lambda';
// import { StringParameter } from '@aws-cdk/aws-ssm';
import * as path from 'path';

export class stackSettings {
readonly stacksettings?: {
Expand All @@ -20,23 +24,43 @@ export class S3SqsStack extends Stack {
const stack = Stack.of(this);

const bucket = new Bucket(this, "myBucket", {
bucketName: 'ets'+'-'+stack.account+'-'+ stackconfig?.stacksettings?.environment +'-'+'s3-bucket',
bucketName: stack.account+'-'+ stackconfig?.stacksettings?.environment +'-'+'s3-bucket',
removalPolicy : RemovalPolicy.DESTROY});

// const ext_q_arn = StringParameter.fromStringParameterAttributes(this, 'ext-account', {
// parameterName: '/sqs/ext-account-id',
// });

// const ext_q_name = 'arn:aws:sqs:us-east-1:'+ ext_q_arn.stringValue +':cross-prsnlaccount-test-sqs';

const my_queue = new Queue(this, 'mySqs', {
queueName: 'ets'+'-'+stack.account+'-'+ stackconfig?.stacksettings?.environment +'-'+'testQueue',
queueName: stack.account+'-'+ stackconfig?.stacksettings?.environment +'-'+'testQueue',
visibilityTimeout: Duration.seconds(300),
retentionPeriod: Duration.seconds(1209600)
});

// const second_queue = Queue.fromQueueArn(this, 'SecondSqs', ext_q_name);

bucket.addEventNotification(EventType.OBJECT_CREATED,
new SqsDestination(my_queue));

const lambda = new Function(this, 'Lambda', {
memorySize: 512,
code: Code.fromAsset(path.resolve(__dirname, '../function')),
handler: 'index.handler',
runtime: Runtime.NODEJS_10_X,
});

lambda.addEventSource(new SqsEventSource(my_queue, {
batchSize: 1
}));

// role and policy for Lambda to read from above bucket
const role = new Role(this, 'myRole', {
assumedBy: new ServicePrincipal('lambda.amazonaws.com'),
});


role.addToPolicy(new PolicyStatement({
resources: [ bucket.bucketArn, bucket.bucketArn + '//*' ],
actions: ['s3:Get*', 's3:List'] }));
Expand Down
Loading