@certenza/aws-cdk-infrastructure-commons 2.0.2 → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/apigateway.js +17 -73
- package/package.json +1 -1
package/dist/src/apigateway.js
CHANGED
|
@@ -67,86 +67,30 @@ const createApiGateway = (scope, apiName, domainName, hostedZoneId, zoneName) =>
|
|
|
67
67
|
const hostedZone = (0, route53_1.getHostedZone)(scope, "HostedZone", hostedZoneId, zoneName);
|
|
68
68
|
// Create a certificate for the API Gateway domain
|
|
69
69
|
const certificate = (0, acm_1.createCertificate)(scope, `ApiGatewayCertificate`, domainName, hostedZone);
|
|
70
|
-
// Create
|
|
71
|
-
const accessLogGroup = new logs.LogGroup(scope, `${apiName}-AccessLogs`, {
|
|
72
|
-
retention: logs.RetentionDays.ONE_WEEK,
|
|
73
|
-
});
|
|
74
|
-
// Create RestApi without auto-deployment so we can control the order of resource creation
|
|
70
|
+
// Create the API Gateway
|
|
75
71
|
const api = new apigateway.RestApi(scope, apiName, {
|
|
76
72
|
description: `Public API Gateway for ${apiName}`,
|
|
77
73
|
endpointTypes: [apigateway.EndpointType.REGIONAL],
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
])),
|
|
93
|
-
retention: logs.RetentionDays.ONE_MONTH, // Budget-friendly: 1 month retention
|
|
94
|
-
removalPolicy: cdk.RemovalPolicy.DESTROY, // Destroy log group when API is deleted
|
|
95
|
-
});
|
|
96
|
-
// Create deployment that automatically updates when API changes
|
|
97
|
-
// To ensure the stage updates when new methods/resources are added, we include
|
|
98
|
-
// the API root resource ID in the deployment description. This forces CloudFormation
|
|
99
|
-
// to recognize when the API changes and update both the deployment and stage.
|
|
100
|
-
const deployment = new apigateway.Deployment(scope, `${apiName}-Deployment`, {
|
|
101
|
-
api: api,
|
|
102
|
-
retainDeployments: false,
|
|
103
|
-
});
|
|
104
|
-
// Add dependency on API root to ensure deployment updates when API changes
|
|
105
|
-
// This ensures the deployment is recreated when methods/resources are added
|
|
106
|
-
deployment.node.addDependency(api.root);
|
|
107
|
-
// Ensure stage depends on deployment to update when deployment changes
|
|
108
|
-
// This forces the stage to update its deployment reference when a new deployment is created
|
|
109
|
-
// Create stage with logging configuration
|
|
110
|
-
// The execution log group must exist before the stage is created so API Gateway uses it
|
|
111
|
-
// IMPORTANT: The stage must reference the deployment. When a new deployment is created
|
|
112
|
-
// (when API methods/resources change), CloudFormation should update the stage automatically.
|
|
113
|
-
// However, to ensure this works, we add an explicit dependency and include deployment info.
|
|
114
|
-
const stage = new apigateway.Stage(scope, `${apiName}-Stage`, {
|
|
115
|
-
deployment: deployment,
|
|
116
|
-
stageName: "prod",
|
|
117
|
-
loggingLevel: apigateway.MethodLoggingLevel.INFO,
|
|
118
|
-
dataTraceEnabled: false,
|
|
119
|
-
accessLogDestination: new apigateway.LogGroupLogDestination(accessLogGroup),
|
|
120
|
-
});
|
|
121
|
-
// Explicitly add dependency to ensure stage updates when deployment changes
|
|
122
|
-
// This is necessary because CloudFormation might not detect the change automatically
|
|
123
|
-
stage.node.addDependency(deployment);
|
|
124
|
-
// Ensure the execution log group is created before the stage
|
|
125
|
-
// This is necessary because the stage doesn't reference the log group directly,
|
|
126
|
-
// but API Gateway needs it to exist when the stage is created
|
|
127
|
-
stage.node.addDependency(executionLogGroup);
|
|
128
|
-
// Create custom domain name with TLS 1.2 security policy
|
|
129
|
-
// CDK automatically ensures the certificate is validated before creating the domain
|
|
130
|
-
const apiDomain = new apigateway.DomainName(scope, `${apiName}-Domain`, {
|
|
131
|
-
domainName: domainName,
|
|
132
|
-
certificate: certificate,
|
|
133
|
-
securityPolicy: apigateway.SecurityPolicy.TLS_1_2, // Enforce TLS 1.2 minimum
|
|
134
|
-
endpointType: apigateway.EndpointType.REGIONAL,
|
|
135
|
-
});
|
|
136
|
-
// Map the custom domain to the API stage
|
|
137
|
-
// CDK automatically handles dependencies: BasePathMapping depends on apiDomain, api, and stage
|
|
138
|
-
new apigateway.BasePathMapping(scope, `${apiName}-BasePathMapping`, {
|
|
139
|
-
domainName: apiDomain,
|
|
140
|
-
restApi: api,
|
|
141
|
-
stage: stage,
|
|
74
|
+
domainName: {
|
|
75
|
+
domainName: domainName,
|
|
76
|
+
certificate: certificate,
|
|
77
|
+
securityPolicy: apigateway.SecurityPolicy.TLS_1_2,
|
|
78
|
+
endpointType: apigateway.EndpointType.REGIONAL,
|
|
79
|
+
},
|
|
80
|
+
deployOptions: {
|
|
81
|
+
loggingLevel: apigateway.MethodLoggingLevel.OFF,
|
|
82
|
+
dataTraceEnabled: false,
|
|
83
|
+
accessLogDestination: new apigateway.LogGroupLogDestination(new logs.LogGroup(scope, `${apiName}-AccessLogs`, {
|
|
84
|
+
removalPolicy: cdk.RemovalPolicy.DESTROY,
|
|
85
|
+
retention: logs.RetentionDays.ONE_WEEK,
|
|
86
|
+
})),
|
|
87
|
+
},
|
|
142
88
|
});
|
|
143
|
-
// Create Route53 A record pointing to the API Gateway
|
|
144
|
-
// CDK automatically handles dependency: Route53 record waits for domain to be created
|
|
145
|
-
// via the alias target (ApiGatewayDomain)
|
|
89
|
+
// Create Route53 A record pointing to the API Gateway
|
|
146
90
|
new route53.ARecord(scope, `${apiName}-AliasRecord`, {
|
|
147
91
|
zone: hostedZone,
|
|
148
92
|
recordName: domainName.replace(`${hostedZone.zoneName}.`, ""),
|
|
149
|
-
target: route53.RecordTarget.fromAlias(new route53targets.
|
|
93
|
+
target: route53.RecordTarget.fromAlias(new route53targets.ApiGateway(api)),
|
|
150
94
|
});
|
|
151
95
|
// Return the API Gateway
|
|
152
96
|
return api;
|
package/package.json
CHANGED