Skip to content

Instantly share code, notes, and snippets.

View markilott's full-sized avatar

Mark Ilott markilott

  • 20:27 (UTC +07:00)
View GitHub Profile
// Full project available here: https://github.com/markilott/aws-cdk-lambda-powertools
import { Stack } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { QueryDefinition, QueryString } from 'aws-cdk-lib/aws-logs';
import {
Color, Dashboard, GraphWidget, GraphWidgetView, LegendPosition, Metric, Row, TextWidget,
} from 'aws-cdk-lib/aws-cloudwatch';
import { CfnGroup } from 'aws-cdk-lib/aws-xray';
// ... import local types
/** Full code is available here: https://github.com/markilott/aws-cdk-lambda-powertools/tree/main/lib/constructs/custom-nodejs-fnc */
import { Construct } from 'constructs';
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
import { Runtime, Tracing } from 'aws-cdk-lib/aws-lambda';
import { Duration } from 'aws-cdk-lib';
import { ILogGroup } from 'aws-cdk-lib/aws-logs';
import {
Color, GaugeWidget, GraphWidget, GraphWidgetView, IWidget, LogQueryVisualizationType, LogQueryWidget, MathExpression, Metric, SingleValueWidget, TextWidget,
} from 'aws-cdk-lib/aws-cloudwatch';
import { CustomFunctionProps, PowerToolsEnvProps } from './types';
/** Full code available here: https://github.com/markilott/aws-cdk-lambda-powertools/blob/main/src/lambda/read-data/index.ts */
import { AWSError, DynamoDB } from 'aws-sdk';
import { Logger, injectLambdaContext } from '@aws-lambda-powertools/logger';
import { Metrics, MetricUnits, logMetrics } from '@aws-lambda-powertools/metrics';
import { Tracer, captureLambdaHandler } from '@aws-lambda-powertools/tracer';
import middy from '@middy/core';
/** Instantiate the PowerTools instances */
const logger = new Logger();
const tracer = new Tracer();
import { Duration, RemovalPolicy } from 'aws-cdk-lib';
import { AttributeType, BillingMode, Table } from 'aws-cdk-lib/aws-dynamodb';
import { Construct } from 'constructs';
type CustomDynamoTableProps = {
/** Table Partition Key */
partitionKey: string,
/** Optional Sort Key */
sortKey?: string,
/** Table Name */
@markilott
markilott / aws-cdk-pipelines-github.js
Created March 23, 2022 14:39
AWS CDK Pipeline GitHub Source
export class PipelineStack extends Stack {
/**
* Creates a deployment Pipeline.
* Can be run for each environment to create separate
* pipelines for each.
*
* @param {Construct} scope
* @param {string} id
* @param {StackProps=} props
*/
@markilott
markilott / aws-cdk-pipelines-github-connection.js
Created March 23, 2022 14:23
AWS CDK Pipelines GitHub Connections
// Connecting to GitHub as source in a CDK Pipeline
// Method 1 - Personal Access Token and Secret.
// Create a Personal Access Token in GitHub.
// Store the Token in a Secrets Manager Secret, then:
new CodePipeline(this, 'DemoPipeline', {
synth: new ShellStep('Synth', {
input: CodePipelineSource.gitHub('owner/reponame', 'branchname', {
authentication: SecretValue.secretsManager('SecretName', { jsonField: 'TOKEN_FIELD' }),
trigger: GitHubTrigger.WEBHOOK,
@markilott
markilott / aws-cdk-pipeline-role.js
Created March 22, 2022 13:47
AWS CDK Pipeline Cross Account Role
// PipelinePrepStack in the Tools Account ===================
// Base Role for pipelines. Created here as it is required outside of the pipeline stack for cross-region deployments.
const pipelineBaseRole = new Role(this, 'pipelineBaseRole', {
assumedBy: new ServicePrincipal('codepipeline.amazonaws.com'),
roleName: codeCommitAccessRoleName, // We use this fixed name to attach the role in the pipeline stack
description: 'Role used by CodePipelines to allow for cross-account deployments',
});
pipelineBaseRole.addToPolicy(new PolicyStatement({
sid: 'AssumeRoles',
@markilott
markilott / aws-cdk-pipeline-event-rule.js
Created March 22, 2022 13:38
AWS CDK Cross Account Pipeline Event Rule
// PipelinePrepStack in the Tools Account ===================
// Allow CodeCommit account EventBus to put events to Pipeline account EventBus
// This is used to trigger the pipeline from CodeCommit updates in the Development account
new CfnEventBusPolicy(this, 'eventsPolicy', {
statementId: 'CodeCommit',
eventBusName: 'default',
statement: {
Effect: 'Allow',
Principal: { AWS: `arn:aws:iam::${codeCommitAccount}:root` },
@markilott
markilott / aws-cdk-sso-permission-set.js
Last active February 27, 2022 08:42
AWS CDK SSO Permission Sets
// Create Permission Sets and Assign to Groups and Accounts
// Full code available here: https://github.com/markilott/aws-cdk-sso-permission-sets
// List of Accounts in the Organisation
const accountList = {
master: '123456789',
prod: '123456789',
dev: '123456789',
};
@markilott
markilott / aws-dynamodb-query-examples.js
Created October 24, 2021 06:58
DynamoDB Query Examples in Javascript
const AWS = require('aws-sdk');
const moment = require('moment');
const docClient = new AWS.DynamoDB.DocumentClient({
region: process.env.AWS_REGION,
});
// Query where we have used a Reserved Word as the Partition Key
async function getCollectionList(collection) {
/**