Skip to content

Instantly share code, notes, and snippets.

@yeisoncruz16
Last active November 18, 2020 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yeisoncruz16/ad736f6cca6356faa5962488aaf2ccc8 to your computer and use it in GitHub Desktop.
Save yeisoncruz16/ad736f6cca6356faa5962488aaf2ccc8 to your computer and use it in GitHub Desktop.
Simple NodeJs lambda function to get SQS counts using CDK
{
"app": "node resources"
}
const AWS = require('aws-sdk');
AWS.config.update({
region: 'us-west-2'
});
const sqs = new AWS.SQS({
apiVersion: '2012-11-05'
});
exports.handler = async () => {
const params = {
QueueUrl: process.env.QUEUE_URL,
AttributeNames: ['ApproximateNumberOfMessages'],
};
const result = await new Promise((resolve, reject) => {
sqs.getQueueAttributes(params, (error, data) => {
if (error) {
reject("Error", error);
} else {
resolve(data);
}
});
});
return {
statusCode: 200,
headers: {
"Access-Control-Allow-Headers" : "Content-Type",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET"
},
body: JSON.stringify({result})
};
};
deploy:
@cdk deploy --require-approval never
install:
@npm install
install_production:
@npm install --only=prod
watch:
@tsc -w
build:
@tsc && cdk synth > template.yml
static_test:
bash CI-CD/static_test.sh
clean:
@rm -rf node_modules
@rm -f template.yml &> /dev/null
{
"name": "cools-name-information",
"version": "1.0.0",
"description": "Cool description.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node": "^10.17.0",
"aws-cdk": "^1.69.0",
"typescript": "~3.7.2"
},
"dependencies": {
"@aws-cdk/aws-apigateway": "^1.69.0",
"@aws-cdk/aws-lambda": "*",
"@aws-cdk/core": "*",
"aws-sdk": "^2.776.0"
}
}
import lambda = require('@aws-cdk/aws-lambda');
import cdk = require('@aws-cdk/core');
import iam = require("@aws-cdk/aws-iam");
import * as apigateway from "@aws-cdk/aws-apigateway";
import fs = require('fs');
export class LambdaCronStack extends cdk.Stack {
constructor(app: cdk.App, id: string) {
super(app, id);
const policy = new iam.PolicyStatement({
effect: iam.Effect.ALLOW
});
policy.addResources('*');
policy.addActions(
'sqs:GetQueueAttributes',
'sqs:List*',
'sqs:ChangeMessageVisibility');
const handler = new lambda.Function(this, 'Singleton', {
code: new lambda.InlineCode(fs.readFileSync('main.js', {encoding: 'utf-8'})),
handler: 'index.handler',
memorySize: 128,
timeout: cdk.Duration.seconds(10),
runtime: lambda.Runtime.NODEJS_12_X,
environment: {
QUEUE_URL: 'https://sqs.us-west-2.amazonaws.com/321654987/queue-name'
},
functionName: 'this-is-the-pretty-name',
description: 'this is the pretty description.',
initialPolicy: [ policy ]
});
const api = new apigateway.RestApi(this, "widgets-api", {
restApiName: "this-is-thepretty-api-name",
description: "Cool description.",
defaultCorsPreflightOptions: {
allowOrigins: apigateway.Cors.ALL_ORIGINS
}
});
const getWidgetsIntegration = new apigateway.LambdaIntegration(handler, {
requestTemplates: { "application/json": '{ "statusCode": "200" }' }
});
api.root.addMethod("GET", getWidgetsIntegration);
}
}
const app = new cdk.App();
new LambdaCronStack(app, 'cool-stack-name');
app.synth();
{
"compilerOptions": {
"target":"ES2018",
"module": "commonjs",
"lib": ["es2016", "es2017.object", "es2017.string"],
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": false,
"inlineSourceMap": true,
"inlineSources": true,
"experimentalDecorators": true,
"strictPropertyInitialization":false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment