-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: allow invoking built CDK synthesized templates #3549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1956922
6828f5a
b8c2118
88621a5
a73654b
a7278e3
6170d15
494d99e
dff49f5
f1c8e72
70d1955
dafa688
1047d05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,17 +9,24 @@ | |
|
|
||
| from samcli.lib.iac.cdk.utils import is_cdk_project | ||
|
|
||
| from samcli.lib.utils.resources import AWS_CLOUDFORMATION_STACK | ||
|
|
||
| CDK_NESTED_STACK_RESOURCE_ID_SUFFIX = ".NestedStack" | ||
|
|
||
| RESOURCES_KEY = "Resources" | ||
| PROPERTIES_KEY = "Properties" | ||
| METADATA_KEY = "Metadata" | ||
|
|
||
| RESOURCE_CDK_PATH_METADATA_KEY = "aws:cdk:path" | ||
| ASSET_PATH_METADATA_KEY = "aws:asset:path" | ||
| ASSET_PROPERTY_METADATA_KEY = "aws:asset:property" | ||
|
|
||
| IMAGE_ASSET_PROPERTY = "Code.ImageUri" | ||
| ASSET_DOCKERFILE_PATH_KEY = "aws:asset:dockerfile-path" | ||
| ASSET_DOCKERFILE_BUILD_ARGS_KEY = "aws:asset:docker-build-args" | ||
|
|
||
| SAM_RESOURCE_ID_KEY = "SamResourceId" | ||
| SAM_IS_NORMALIZED = "SamNormalized" | ||
| SAM_METADATA_DOCKERFILE_KEY = "Dockerfile" | ||
| SAM_METADATA_DOCKER_CONTEXT_KEY = "DockerContext" | ||
| SAM_METADATA_DOCKER_BUILD_ARGS_KEY = "DockerBuildArgs" | ||
|
|
@@ -53,18 +60,21 @@ def normalize(template_dict, normalize_parameters=False): | |
|
|
||
| for logical_id, resource in resources.items(): | ||
| resource_metadata = resource.get(METADATA_KEY, {}) | ||
| asset_property = resource_metadata.get(ASSET_PROPERTY_METADATA_KEY) | ||
|
|
||
| if asset_property == IMAGE_ASSET_PROPERTY: | ||
| asset_metadata = ResourceMetadataNormalizer._extract_image_asset_metadata(resource_metadata) | ||
| ResourceMetadataNormalizer._update_resource_metadata(resource_metadata, asset_metadata) | ||
| # For image-type functions, the asset path is expected to be the name of the Docker image. | ||
| # When building, we set the name of the image to be the logical id of the function. | ||
| asset_path = logical_id.lower() | ||
| else: | ||
| asset_path = resource_metadata.get(ASSET_PATH_METADATA_KEY) | ||
|
|
||
| ResourceMetadataNormalizer._replace_property(asset_property, asset_path, resource, logical_id) | ||
| is_normalized = resource_metadata.get(SAM_IS_NORMALIZED, False) | ||
| if not is_normalized: | ||
| asset_property = resource_metadata.get(ASSET_PROPERTY_METADATA_KEY) | ||
| if asset_property == IMAGE_ASSET_PROPERTY: | ||
| asset_metadata = ResourceMetadataNormalizer._extract_image_asset_metadata(resource_metadata) | ||
| ResourceMetadataNormalizer._update_resource_metadata(resource_metadata, asset_metadata) | ||
| # For image-type functions, the asset path is expected to be the name of the Docker image. | ||
| # When building, we set the name of the image to be the logical id of the function. | ||
| asset_path = logical_id.lower() | ||
| else: | ||
| asset_path = resource_metadata.get(ASSET_PATH_METADATA_KEY) | ||
|
|
||
| ResourceMetadataNormalizer._replace_property(asset_property, asset_path, resource, logical_id) | ||
| if asset_path and asset_property: | ||
| resource_metadata[SAM_IS_NORMALIZED] = True | ||
|
|
||
| # Set SkipBuild metadata iff is-bundled metadata exists, and value is True | ||
| skip_build = resource_metadata.get(ASSET_BUNDLED_METADATA_KEY, False) | ||
|
|
@@ -182,3 +192,65 @@ def _update_resource_metadata(metadata, updated_values): | |
| """ | ||
| for key, val in updated_values.items(): | ||
| metadata[key] = val | ||
|
|
||
| @staticmethod | ||
| def get_resource_id(resource_properties, logical_id): | ||
| """ | ||
| Get unique id for a resource. | ||
| for any resource, the resource id can be the customer defined id if exist, if not exist it can be the | ||
| cdk-defined resource id, or the logical id if the resource id is not found. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| resource_properties dict | ||
| Properties of this resource | ||
| logical_id str | ||
| LogicalID of the resource | ||
|
|
||
| Returns | ||
| ------- | ||
| str | ||
| The unique function id | ||
| """ | ||
| resource_metadata = resource_properties.get("Metadata", {}) | ||
| customer_defined_id = resource_metadata.get(SAM_RESOURCE_ID_KEY) | ||
|
|
||
| if isinstance(customer_defined_id, str) and customer_defined_id: | ||
| LOG.debug( | ||
| "Sam customer defined id is more priority than other IDs. Customer defined id for resource %s is %s", | ||
| logical_id, | ||
| customer_defined_id, | ||
| ) | ||
| return customer_defined_id | ||
|
|
||
| resource_cdk_path = resource_metadata.get(RESOURCE_CDK_PATH_METADATA_KEY) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method and filename is named generic (
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am working on another PR that is related to this part (IaC ids) to use it in other commands. Can I work on this refactoring after I merge this PR, and the one I am working on, as I do not want to miss things, it will be easier to refactor everything together |
||
|
|
||
| if not isinstance(resource_cdk_path, str) or not resource_cdk_path: | ||
| LOG.debug( | ||
| "There is no customer defined id or cdk path defined for resource %s, so we will use the resource " | ||
| "logical id as the resource id", | ||
| logical_id, | ||
| ) | ||
| return logical_id | ||
|
|
||
| # aws:cdk:path metadata format of functions: {stack_id}/{function_id}/Resource | ||
| # Design doc of CDK path: https://github.com/aws/aws-cdk/blob/master/design/construct-tree.md | ||
| cdk_path_partitions = resource_cdk_path.split("/") | ||
|
|
||
| LOG.debug("CDK Path for resource %s is %s", logical_id, cdk_path_partitions) | ||
|
|
||
| if len(cdk_path_partitions) < 2: | ||
| LOG.warning( | ||
| "Cannot detect function id from aws:cdk:path metadata '%s', using default logical id", resource_cdk_path | ||
| ) | ||
| return logical_id | ||
|
|
||
| cdk_resource_id = cdk_path_partitions[-2] | ||
|
|
||
| # Check if the Resource is nested Stack | ||
| if resource_properties.get("Type", "") == AWS_CLOUDFORMATION_STACK and cdk_resource_id.endswith( | ||
| CDK_NESTED_STACK_RESOURCE_ID_SUFFIX | ||
| ): | ||
| cdk_resource_id = cdk_resource_id[: -len(CDK_NESTED_STACK_RESOURCE_ID_SUFFIX)] | ||
|
|
||
| return cdk_resource_id | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| ARG BASE_RUNTIME | ||
|
|
||
| FROM public.ecr.aws/lambda/python:$BASE_RUNTIME | ||
|
|
||
| ARG FUNCTION_DIR="/var/task" | ||
|
|
||
| RUN mkdir -p $FUNCTION_DIR | ||
|
|
||
| COPY main.py $FUNCTION_DIR | ||
|
|
||
| COPY __init__.py $FUNCTION_DIR | ||
|
|
||
| COPY requirements.txt $FUNCTION_DIR | ||
|
|
||
| RUN python -m pip install -r $FUNCTION_DIR/requirements.txt -t $FUNCTION_DIR |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import numpy | ||
|
|
||
| # from cryptography.fernet import Fernet | ||
|
|
||
|
|
||
| def handler(event, context): | ||
|
|
||
| # Try using some of the modules to make sure they work & don't crash the process | ||
| # print(Fernet.generate_key()) | ||
|
|
||
| return {"pi": "{0:.2f}".format(numpy.pi)} | ||
|
|
||
|
|
||
| def first_function_handler(event, context): | ||
| return "Hello World" | ||
|
|
||
|
|
||
| def second_function_handler(event, context): | ||
| return "Hello Mars" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| numpy<1.20.4 |
Uh oh!
There was an error while loading. Please reload this page.