Skip to content

API - API Gateway module

This module creates the API Gateway and all resources.

Example

Typical Usage::

from stacks.resources.api_gateway import api_gateway

created_api_gateway = api_gateway.create_api_gateway(
    self, 
    "WaReviewRemediationItemsApiGateway", 
    remediation_items_table_arn, 
    reports_table_arn, 
    customers_table_arn,
    choices_table_arn,
    generate_report_function_arn, 
    wa_workload_function_arn,
    customer_credentials_function_arn,
    question_updater_function_arn,
    api_doc_function_arn,
    admin_userpool_arn
)

create_api_gateway(stack, name, dynamodb_remediation_items_table_arn, dynamodb_reports_table_arn, dynamodb_customers_table_arn, dynamodb_choices_table_arn, dynamodb_workload_tracker_table_arn, generate_report_function_arn, wa_workload_function_arn, customer_credentials_function_arn, question_updater_function_arn, api_doc_function_arn, admin_userpool_arn)

Create the API Gateway, all resource and methods.

Attributes:

Name Type Description
stack aws_cdk.core.Stack

CDK Stack.

name str

Name of the API Gateway

dynamodb_remediation_items_table_arn str

ARN of the DynamoDB Table with the Remediation Items.

dynamodb_reports_table_arn str

ARN of the DynamoDB Table with the Reports.

dynamodb_customers_table_arn str

ARN of the DynamoDB Table with the Customers.

dynamodb_choices_table_arn str

ARN of the DynamoDB Table with the Choices.

generate_report_function_arn str

ARN of the Lambda Function for generating the Reports.

wa_workload_function_arn str

ARN of the Lambda Function for getting the WA Worklodas from AWS.

customer_credentials_function_arn str

ARN of the Lambda Function for getting and updating the Customer Credentials.

question_updater_function_arn str

ARN of the Lambda Function to update the Choices / Questions.

api_doc_function_arn str

ARN of the Lambda Function to serve the API Dos.

admin_userpool_arn str

Cognito User Pool ARN used for the endpoint authentication.

Returns:

Type Description
apigateway.RestApi

aws_cdk.aws_apigateway.RestApi): The created Rest API Gateway.

Source code in infrastructure/stacks/resources/api_gateway/api_gateway.py
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
def create_api_gateway(
    stack: core.Stack, 
    name: str, 
    dynamodb_remediation_items_table_arn: str, 
    dynamodb_reports_table_arn: str, 
    dynamodb_customers_table_arn: str, 
    dynamodb_choices_table_arn: str, 
    dynamodb_workload_tracker_table_arn: str,
    generate_report_function_arn: str, 
    wa_workload_function_arn: str, 
    customer_credentials_function_arn: str, 
    question_updater_function_arn: str,
    api_doc_function_arn: str,
    admin_userpool_arn: str) -> apigateway.RestApi:

    """Create the API Gateway, all resource and methods.

    Attributes:
        stack (aws_cdk.core.Stack): CDK Stack.
        name (str): Name of the API Gateway
        dynamodb_remediation_items_table_arn (str): ARN of the DynamoDB Table with the Remediation Items.
        dynamodb_reports_table_arn (str): ARN of the DynamoDB Table with the Reports.
        dynamodb_customers_table_arn (str): ARN of the DynamoDB Table with the Customers.
        dynamodb_choices_table_arn (str): ARN of the DynamoDB Table with the Choices.
        generate_report_function_arn (str): ARN of the Lambda Function for generating the Reports.
        wa_workload_function_arn (str): ARN of the Lambda Function for getting the WA Worklodas from AWS.
        customer_credentials_function_arn (str): ARN of the Lambda Function for getting and updating the Customer Credentials.
        question_updater_function_arn (str): ARN of the Lambda Function to update the Choices / Questions.
        api_doc_function_arn (str): ARN of the Lambda Function to serve the API Dos.
        admin_userpool_arn (str): Cognito User Pool ARN used for the endpoint authentication.

    Returns:
        aws_cdk.aws_apigateway.RestApi): The created Rest API Gateway.

    """

    admin_userpool = UserPool.from_user_pool_arn(stack, "WaReviewToolAdminUserPool",user_pool_arn = admin_userpool_arn)

    auth = apigateway.CognitoUserPoolsAuthorizer(stack, "WaReviewToolApiGatewayAuth",
        cognito_user_pools=[admin_userpool]
    )

    hosted_zoned_id = stack.node.try_get_context("hosted_zoned_id")
    hosted_zoned_name = stack.node.try_get_context("hosted_zoned_name")

    hosted_zone = PublicHostedZone.from_hosted_zone_attributes(stack, "WaReviewToolHostedZone", hosted_zone_id=hosted_zoned_id, zone_name=hosted_zoned_name)

    api_cert = DnsValidatedCertificate(stack, "WaReviewToolApiCert",
        domain_name=f"api.{hosted_zoned_name}",
        hosted_zone=hosted_zone
    )

    created_api_gateway = apigateway.RestApi(stack, name,
        deploy=True,
        default_cors_preflight_options=apigateway.CorsOptions(
           allow_origins=apigateway.Cors.ALL_ORIGINS,
            allow_methods=apigateway.Cors.ALL_METHODS
            ),
        endpoint_configuration=apigateway.EndpointConfiguration(types=[apigateway.EndpointType.REGIONAL]),
        deploy_options=apigateway.StageOptions(
            tracing_enabled=True,
            data_trace_enabled=True,
            logging_level=apigateway.MethodLoggingLevel.INFO,
            metrics_enabled=True
        ),
        domain_name=apigateway.DomainNameOptions(
            domain_name=f"api.{hosted_zoned_name}",
            certificate=api_cert
        )
    )

    domain_name = ARecord(stack, "WaReviewToolApiRoute53Alias",
        zone=hosted_zone,
        record_name=f"api.{hosted_zoned_name}",
        target=RecordTarget.from_alias(ApiGateway(created_api_gateway))
    )

    items.create(stack, created_api_gateway, dynamodb_remediation_items_table_arn, auth)
    report_api_stack = ReportApiStack(stack, "ReportApiStack", created_api_gateway=created_api_gateway, dynamodb_reports_table_arn=dynamodb_reports_table_arn, generate_report_function_arn=generate_report_function_arn, auth=auth)
    workloads_api_stack = WorkloadsApiStack(stack, "WorkloadsApiStack", created_api_gateway, wa_workload_function_arn, dynamodb_workload_tracker_table_arn, auth )
    lense_api_stack = LenseApiStack(stack, "LenseApiStack", created_api_gateway, auth)
    choices_api_stack = ChoicesApiStack(stack, "ChoicesApiStack", created_api_gateway, dynamodb_choices_table_arn, question_updater_function_arn, auth)
    customers_api_stack = CustomersApiStack(stack, "CustomersApiStack", created_api_gateway, dynamodb_customers_table_arn, customer_credentials_function_arn, auth)
    docs_api_stack = DocsApiStack(stack, "DocsApiStack", created_api_gateway, api_doc_function_arn, auth)    

    core.Tags.of(created_api_gateway).add("WaReviewTool", "RestApi")
    core.CfnOutput(stack, "WaReviewRemediationItemApiGatewayCustomDomainName", value=domain_name.domain_name, description="", export_name="WaReviewRemediationItemApiGatewayCustomDomainName")

    return created_api_gateway