@certenza/aws-cdk-infrastructure-commons 2.0.2 → 2.0.4

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.
@@ -67,86 +67,34 @@ 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 access log group
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
- deploy: false, // Don't auto-deploy - we'll create deployment and stage manually
79
- });
80
- // Create execution log group with the exact name API Gateway expects
81
- // API Gateway creates execution logs in the format: API-Gateway-Execution-Logs_{api-id}/{stage-name}
82
- // By creating it first, API Gateway will use our log group instead of creating a new one
83
- // Note: The log group name depends on api.restApiId (a token), so CloudFormation will
84
- // create the API first to resolve the token, then create the log group with the resolved name
85
- const executionLogGroup = new logs.LogGroup(scope, `${apiName}-ExecutionLogs`, {
86
- logGroupName: cdk.Token.asString(cdk.Fn.join("/", [
87
- cdk.Fn.join("_", [
88
- "API-Gateway-Execution-Logs",
89
- api.restApiId,
90
- ]),
91
- "prod", // Default stage name
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),
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
+ },
120
88
  });
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,
142
- });
143
- // Create Route53 A record pointing to the API Gateway custom domain
144
- // CDK automatically handles dependency: Route53 record waits for domain to be created
145
- // via the alias target (ApiGatewayDomain)
89
+ // Add a dependency on the API root to help ensure deployments update
90
+ if (api.deploymentStage) {
91
+ api.deploymentStage.node.addDependency(api.root);
92
+ }
93
+ // Create Route53 A record pointing to the API Gateway
146
94
  new route53.ARecord(scope, `${apiName}-AliasRecord`, {
147
95
  zone: hostedZone,
148
96
  recordName: domainName.replace(`${hostedZone.zoneName}.`, ""),
149
- target: route53.RecordTarget.fromAlias(new route53targets.ApiGatewayDomain(apiDomain)),
97
+ target: route53.RecordTarget.fromAlias(new route53targets.ApiGateway(api)),
150
98
  });
151
99
  // Return the API Gateway
152
100
  return api;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@certenza/aws-cdk-infrastructure-commons",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "description": "Common infrastructure reusable utilities and resources for Certenza projects",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",