Skip to content

Instantly share code, notes, and snippets.

@apoorvmote
Last active March 4, 2021 07:33
Show Gist options
  • Save apoorvmote/1a653acfb77ec11771e904089c4c8dc0 to your computer and use it in GitHub Desktop.
Save apoorvmote/1a653acfb77ec11771e904089c4c8dc0 to your computer and use it in GitHub Desktop.
new ARecord(this, 'apiAliasRecord', {
zone: hostedZone,
target: RecordTarget.fromAlias(new ApiGatewayv2Domain(domain))
})
const hostedZone = HostedZone.fromHostedZoneAttributes(this, 'hostedZoneWithAttributes', {
hostedZoneId,
zoneName: website_domain
})
const apiCert = new DnsValidatedCertificate(this, 'ApiSSL', {
domainName: api_domain,
hostedZone
})
const existingAPI = HttpApi.fromHttpApiAttributes(this, 'existingAPI', {
apiEndpoint: api_domain,
httpApiId: 'myApiId'
})
const domain = new DomainName(this, 'api_domain', {
domainName: api_domain,
certificate: apiCert
})
const api = new HttpApi(this, 'apiEndpoint', {
defaultDomainMapping: {
domainName: domain
},
corsPreflight: {
allowCredentials: true,
allowHeaders: ['Content-Type'],
allowMethods: [HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE],
allowOrigins: [
'https://example.com'
]
},
apiName: 'exampleAPI',
disableExecuteApiEndpoint: true
})
new CfnOutput(this, 'apiID', {
value: api.httpApiId
})
///////////////////////////////
// Part 1
const hostedZone = HostedZone.fromHostedZoneAttributes(this, 'hostedZoneWithAttributes', {
hostedZoneId,
zoneName: website_domain
})
const certificate = new DnsValidatedCertificate(this, 'ApiSSL', {
domainName: website_domain,
hostedZone
})
const api = new HttpApi(this, 'apiEndpoint', {
apiName: 'exampleAPISameDomain',
})
new CfnOutput(this, 'apiID', {
value: api.apiEndpoint
})
const signUpFn = new Function(this, 'signUpFn', {
runtime: Runtime.NODEJS_14_X,
code: Code.fromAsset(`${__dirname}/../lambda-fns/sign-up/deployment.zip`),
handler: 'index.handler',
memorySize: 512
})
///////////////////////////////
///////////////////////////////
// Part 2
api.addRoutes({
path: '/api/sign-up',
methods: [HttpMethod.POST],
integration: new LambdaProxyIntegration({ handler: signUpFn })
})
const apiOriginPolicy = new OriginRequestPolicy(this, 'apiOriginPolicy', {
cookieBehavior: OriginRequestCookieBehavior.all(),
headerBehavior: OriginRequestHeaderBehavior.none(),
queryStringBehavior: OriginRequestQueryStringBehavior.all()
})
const distribution = new Distribution(this, 'websiteAndAPIDistribution', {
defaultBehavior: {
origin: new HttpOrigin('origin-source-code.com'),
},
additionalBehaviors: {
'api/*': {
origin: new HttpOrigin(api.apiEndpoint.replace('https://', '')),
allowedMethods: AllowedMethods.ALLOW_ALL,
cachePolicy: CachePolicy.CACHING_DISABLED,
compress: false,
originRequestPolicy: apiOriginPolicy,
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS
}
},
domainNames: [website_domain],
certificate,
minimumProtocolVersion: SecurityPolicyProtocol.TLS_V1_2_2019,
enableIpv6: true,
enabled: true,
httpVersion: HttpVersion.HTTP2,
priceClass: PriceClass.PRICE_CLASS_ALL
})
///////////////////////////////
///////////////////////////////
// Part 3
new ARecord(this, 'AliasForCloudfront', {
target: RecordTarget.fromAlias(new CloudFrontTarget(distribution)),
zone: hostedZone,
recordName: website_domain
})
///////////////////////////////
const signUpFn = new Function(this, 'signUpFn', {
runtime: Runtime.NODEJS_14_X,
code: Code.fromAsset(`${__dirname}/../lambda-fns/sign-up/deployment.zip`),
handler: 'index.handler',
memorySize: 512
})
api.addRoutes({
path: '/sign-up',
methods: [HttpMethod.POST],
integration: new LambdaProxyIntegration({ handler: signUpFn })
})
///////////////////////////////
// Part 1
const hostedZone = HostedZone.fromHostedZoneAttributes(this, 'hostedZoneWithAttributes', {
hostedZoneId,
zoneName: website_domain
})
const apiCert = new DnsValidatedCertificate(this, 'ApiSSL', {
domainName: api_domain,
hostedZone
})
///////////////////////////////
///////////////////////////////
// Part 2
const domain = new DomainName(this, 'api_domain', {
domainName: api_domain,
certificate: apiCert
})
const api = new HttpApi(this, 'apiEndpoint', {
defaultDomainMapping: {
domainName: domain
},
corsPreflight: {
allowCredentials: true,
allowHeaders: ['Content-Type'],
allowMethods: [HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE],
allowOrigins: [
'https://example.com'
]
},
apiName: 'exampleAPI',
disableExecuteApiEndpoint: true
})
new CfnOutput(this, 'apiID', {
value: api.httpApiId
})
const signUpFn = new Function(this, 'signUpFn', {
runtime: Runtime.NODEJS_14_X,
code: Code.fromAsset(`${__dirname}/../lambda-fns/sign-up/deployment.zip`),
handler: 'index.handler',
memorySize: 512
})
///////////////////////////////
///////////////////////////////
// Part 3
new ARecord(this, 'apiAliasRecord', {
zone: hostedZone,
target: RecordTarget.fromAlias(new ApiGatewayv2Domain(domain))
})
api.addRoutes({
path: '/sign-up',
methods: [HttpMethod.POST],
integration: new LambdaProxyIntegration({ handler: signUpFn })
})
///////////////////////////////
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment