Skip to content

Instantly share code, notes, and snippets.

View leegilmorecode's full-sized avatar
:atom:
Serverless Hero

Lee Gilmore leegilmorecode

:atom:
Serverless Hero
View GitHub Profile
async function handler(): Promise<{ body: string; statusCode: number }> {
console.log("get-stock.handler - started");
return {
body: JSON.stringify({
stock: [
{
stockId: 1,
description: "hammers",
},
// add a security group for the vpc endpoint
const sg: ec2.SecurityGroup = new ec2.SecurityGroup(this, "stock-vpc-sg", {
vpc,
allowAllOutbound: true,
securityGroupName: "stock-vpc-sg",
});
sg.addIngressRule(ec2.Peer.ipv4(props.cidr), ec2.Port.tcp(443));
// create the vpc endpoint
// create an internal application load balancer
this.stockLoadBalancer = new elbv2.ApplicationLoadBalancer(
this,
"stock-internal-elb",
{
vpc,
http2Enabled: false,
loadBalancerName: "stock-internal-elb",
vpcSubnets: vpc.selectSubnets({
subnetType: ec2.SubnetType.PRIVATE_ISOLATED,
import axios from "axios";
type Stock = {
stockId: number;
description: string;
};
type StockResponse = {
stock: Stock[];
};
@leegilmorecode
leegilmorecode / example.ts
Created December 13, 2021 21:26
Private DNS on VPC with VPC Endpoints
import axios from "axios";
async function handler() {
const { data } = await axios.get(
`https://private-api-id.execute-api.eu-west-1.amazonaws.com/prod/stock`, // 👈 this private call would work
{
headers: {
"x-api-key": "super-secret-api-key",
},
}
@leegilmorecode
leegilmorecode / serverless.yml
Created November 15, 2021 15:05
InputTransformations in EventBridge rule
EventRule:
Type: AWS::Events::Rule
Properties:
Description: 'PayslipUploadedEventRule'
EventBusName: 'HREventBus'
EventPattern:
account:
- !Sub '${AWS::AccountId}'
source:
- 'payslip.uploaded'
@leegilmorecode
leegilmorecode / fulfilment.ts
Created October 4, 2021 12:58
quick example handler to retrieve an EventBridge event
import { SNSEvent, EventBridgeEvent, Handler } from 'aws-lambda';
export const handler: Handler<SNSEvent | EventBridgeEvent<any, any>> = (event) => {
console.log('Fulfilment handler');
console.log('event = ' + JSON.stringify(event));
};
import { APIGatewayProxyHandler, APIGatewayProxyResult, APIGatewayEvent } from 'aws-lambda';
import * as AWS from 'aws-sdk';
import { validate } from '../shared/validator';
import { OrderCreated } from '../../schema/order_create/ordercreated/OrderCreated';
import { OrderCreatedItem } from '../../schema/order_create/ordercreated/OrderCreatedItem';
import * as schema from '../../schema/order_create/ordercreated/[email protected]';
const eventBridge = new AWS.EventBridge({ region: 'eu-west-1' });
export const handler: APIGatewayProxyHandler = async ({ body }: APIGatewayEvent): Promise<APIGatewayProxyResult> => {
@leegilmorecode
leegilmorecode / serverless.yml
Created October 4, 2021 12:48
Example for the team :)
service: serverless-event-bridge
variablesResolutionMode: 20210326
provider:
name: aws
runtime: nodejs14.x
lambdaHashingVersion: 20201221
memorySize: 128
stage: ${opt:stage, 'dev'}
region: ${opt:region, 'eu-west-1'}
eventBridge:
export const randomErrors = (): void | Error => {
if (Math.random() > 0.9) {
throw new Error('spurious error');
}
};