Skip to content

Instantly share code, notes, and snippets.

View maguzzi's full-sized avatar

Marco Aguzzi maguzzi

View GitHub Profile
@maguzzi
maguzzi / cost-control.json
Created January 14, 2024 19:01
Budget and monitor cloudformation stack
{
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": {
"EmailAddressForNotificationParameter": {
"Description": "Email address for the notification",
"Type": "String"
}
},
"Description": "Static website budget",
"Resources": {
@maguzzi
maguzzi / lambda-invalidate.yaml
Last active January 3, 2024 16:55
Template for lambda that invalidates the cloudfront cache
AWSTemplateFormatVersion: "2010-09-09"
Description: Lambda action cloudfront cache invalidation stack
Parameters:
Stage:
Type: String
AllowedValues:
- dev
- prod
@maguzzi
maguzzi / index.py
Created January 1, 2024 19:13
Cloudfront cache invalidation lambda
import boto3
import os
code_pipeline = boto3.client("codepipeline")
cloud_front = boto3.client("cloudfront")
def lambda_handler(event, context):
print(event)
job_id = event["CodePipeline.job"]["id"]
try:
@maguzzi
maguzzi / index.mjs
Created December 9, 2023 17:29
Lambda@Edge for 301 redirect
export const handler = async (event, context, callback) => {
var olduri = event.Records[0].cf.request.uri; // search for the uri in cloudfront request
var host = event.Records[0].cf.request.headers.host.find(o => o.key === 'Host').value; // search for the host in the key-value headers
if (host.startsWith("www.")) {
const response = {
status: '301',
statusDescription: 'Moved Permanently',
headers: {
location: [{
key: 'Location',
@maguzzi
maguzzi / parent.json
Last active January 2, 2024 15:54
Parent cloudformation template
{
"AWSTemplateFormatVersion": "2010-09-09",
"Conditions": {
"AlterativeDomainNotPresent": {
"Fn::Equals": [
"",
{
"Ref": "AlternativeDomainNameParameter"
}
@maguzzi
maguzzi / lambda-edge.yaml
Created November 21, 2023 22:27
Child lambda template
AWSTemplateFormatVersion: "2010-09-09"
Description: Lambda@Edge function stack
Parameters:
Stage:
Type: String
AllowedValues:
- dev
- prod
Resources:
LambdaEdge:
@maguzzi
maguzzi / marcoaguzzi.json
Last active January 2, 2024 17:37
Parametrized codepipeline
{
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": {
"ArtifactS3BucketNameParameter": {
"Description": "Artifact S3 Bucket Name",
"Type": "String"
},
"S3BucketNameParameter": {
"Description": "S3 Bucket Name",
"Type": "String"
regions:
- us-east-1
- global
account-blocklist:
- "1234567890" # don't nuke this
accounts:
"0123456789": {} #nuke this that has an alias, no filter