Created
July 18, 2021 12:02
-
-
Save remie/ef3af4f98a2ad95a1a8ac124a36c36bd to your computer and use it in GitHub Desktop.
Using AWS CloudFront SDK to provision a distribution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const accessKeyId = process.env.AWS_ACCESS_KEY_ID; | |
const secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY; | |
const client = new CloudFront({ | |
credentials: async () => ({ accessKeyId, secretAccessKey }), | |
region: 'us-east-1' | |
}); | |
const { Distribution } = await client.createDistribution({ | |
DistributionConfig: { | |
Enabled: true, | |
Origins: { | |
Quantity: 1, | |
Items: [{ | |
Id: <<ORIGIN_ID>>, | |
DomainName: <<YOUR_DOMAIN>>, | |
OriginPath: <<PATH_PREFIX_YOU_CAN_USE_TO_IDENTIFY_CUSTOMER>>, | |
CustomOriginConfig: { | |
HTTPPort: 80, | |
HTTPSPort: 443, | |
OriginProtocolPolicy: 'match-viewer' | |
} | |
}] | |
}, | |
DefaultCacheBehavior: { | |
TargetOriginId: <<ORIGIN_ID>>, | |
ViewerProtocolPolicy: 'redirect-to-https', | |
CachePolicyId: '658327ea-f89d-4fab-a63d-7e88639e58f6' | |
}, | |
Aliases: { | |
Quantity: 1, | |
Items: [ <<CUSTOMER_DOMAIN>> ] | |
}, | |
ViewerCertificate: { | |
ACMCertificateArn: <<CUSTOMER_DOMAIN_CERTIFICATE_ARN>>, | |
SSLSupportMethod: 'sni-only' | |
}, | |
Comment: `Custom domain support for <<CUSTOMER_DOMAIN>>` | |
} | |
}); | |
// Use the Distribution?.DomainName for the CNAME record of the customer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment