Skip to content

Commit

Permalink
fix: Error when trying to build CDK project with image assets. (#3136)
Browse files Browse the repository at this point in the history
* Fix image assets not building

* Add unit tests for asset resolving

* Remove unused import
  • Loading branch information
mildaniel authored Aug 5, 2021
1 parent 7cd9d27 commit 73673b4
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 30 deletions.
11 changes: 8 additions & 3 deletions samcli/lib/intrinsic_resolver/intrinsic_property_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,14 @@ def resolve_resources_assets(self, ignore_errors):
asset.source_path, ignore_errors, parent_function=asset.source_property
)
elif isinstance(asset, ImageAsset):
asset.source_local_image = self.intrinsic_property_resolver(
asset.source_local_image, ignore_errors, parent_function=asset.source_property
)
if asset.source_local_image:
asset.source_local_image = self.intrinsic_property_resolver(
asset.source_local_image, ignore_errors, parent_function=asset.source_property
)
else:
asset.source_path = self.intrinsic_property_resolver(
asset.source_path, ignore_errors, parent_function=asset.source_property
)

def resolve_attribute(self, cloud_formation_property, ignore_errors=False):
"""
Expand Down
2 changes: 1 addition & 1 deletion samcli/lib/providers/sam_function_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ def _convert_lambda_function_resource(
for asset in assets:
if isinstance(asset, ImageAsset) and asset.source_property.startswith("Code"):
image_asset = asset
code = image_asset.source_local_image
code = image_asset.source_local_image if image_asset.source_local_image else image_asset.source_path
if isinstance(code, str):
function_image_uri = cast(Optional[str], code)
else:
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/buildcmd/test_cdk_build_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ def test_cdk_nested_build(self):
self.verify_included_expected_project_manifest()

# Verify invoke after build
body = f'{{"message":"hello world from Docker"}}'
expected = {"body": body, "statusCode": 200}
self.verify_invoke_built_function("root-stack/container-function", expected, self.working_dir)
expected = get_expected_response(message="hello world")
self.verify_invoke_built_function("root-stack/nested-stack/cdk-wing-test-lambda", expected, self.working_dir)
expected = get_expected_response(message="hello world 2!")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
'use strict';

const AWS = require('aws-sdk');
const log = require('lambda-log');

module.exports.get = (event, context, callback) => {

log.options.debug = true;
log.debug(params);

const response = {
statusCode: 200,
body: JSON.stringify({
"message": "hello world!!! docker"
"message": "hello world from Docker"
})
};
log.debug(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,31 @@
# being updated to use `cdk`. You may delete this import if you don't need it.
from aws_cdk import (
aws_lambda as _lambda,
aws_sam as sam,
aws_iam as _iam,
aws_cloudformation as cfn,
core
core,
)


class RootStack(core.Stack):
def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)

cdk_lambda = _lambda.Function(
cdk_lambda = _lambda.DockerImageFunction(
scope=self,
id="cdk-wing-test-lambda",
runtime=_lambda.Runtime.PYTHON_3_7,
code=_lambda.Code.from_asset("./stack3_lambda_code"),
handler="app.lambda_handler",
id="container-function",
code=_lambda.DockerImageCode.from_image_asset(
"./docker_lambda_code",
cmd=['app.get'],
entrypoint=["/lambda-entrypoint.sh"],
),
)

# cdk_lambda = _lambda.DockerImageFunction(
# scope=self,
# id="container-function",
# code=_lambda.DockerImageCode.from_image_asset(
# "./docker_lambda_code",
# cmd=['app.get'],
# entrypoint=["/lambda-entrypoint.sh"],
# ),
# )

remote_nested_stack = cfn.CfnStack(
scope=self,
id="remote-nested-stack",
template_url="s3://bucket/key",
)

# serverless_function = sam.CfnFunction(
# scope=self,
# id="severless-function",
# code_uri=
# )


class HelloCdkNestedStacksStack(cfn.NestedStack):
Expand Down
68 changes: 68 additions & 0 deletions tests/unit/lib/intrinsic_resolver/test_intrinsic_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from parameterized import parameterized

from samcli.lib.iac.interface import Resource, S3Asset, ImageAsset

from samcli.lib.intrinsic_resolver.intrinsic_property_resolver import IntrinsicResolver
from samcli.lib.intrinsic_resolver.intrinsics_symbol_table import IntrinsicsSymbolTable
from samcli.lib.intrinsic_resolver.invalid_intrinsic_exception import InvalidIntrinsicException
Expand Down Expand Up @@ -1115,3 +1117,69 @@ def lambda_func(x):

resolver.set_intrinsic_key_function_map({"key": lambda_func})
self.assertTrue(resolver.intrinsic_key_function_map.get("key") == lambda_func)


class TestResolveResourceAssets(TestCase):
def setUp(self):
self.source_path = ".aws-sam/.cdk-out/root_stack_template.json"
self.resource = Resource(body={})

def test_resolve_s3_asset(self):
asset = S3Asset()
asset.source_path = {"Ref": "SourcePath"}
self.resource.assets = [asset]

input_template = {
"Parameters": {"SourcePath": {"Default": self.source_path}},
"Resources": {"LambdaFunction": self.resource},
}

output_resource = Resource(
body={},
assets=[S3Asset(source_path=self.source_path)],
)

symbol_resolver = IntrinsicsSymbolTable(template=input_template, logical_id_translator={})
resolver = IntrinsicResolver(template=input_template, symbol_resolver=symbol_resolver)
processed_template = resolver.resolve_template()
self.assertEqual(processed_template["Resources"]["LambdaFunction"], output_resource)

def test_resolve_image_asset(self):
asset = ImageAsset()
asset.source_local_image = {"Ref": "SourceLocalImage"}
self.resource.assets = [asset]

input_template = {
"Parameters": {"SourceLocalImage": {"Default": self.source_path}},
"Resources": {"LambdaFunction": self.resource},
}

output_resource = Resource(
body={},
assets=[ImageAsset(source_local_image=self.source_path)],
)

symbol_resolver = IntrinsicsSymbolTable(template=input_template, logical_id_translator={})
resolver = IntrinsicResolver(template=input_template, symbol_resolver=symbol_resolver)
processed_template = resolver.resolve_template()
self.assertEqual(processed_template["Resources"]["LambdaFunction"], output_resource)

def test_resolve_image_asset_no_image_path(self):
asset = ImageAsset()
asset.source_path = {"Ref": "SourcePath"}
self.resource.assets = [asset]

input_template = {
"Parameters": {"SourcePath": {"Default": self.source_path}},
"Resources": {"LambdaFunction": self.resource},
}

output_resource = Resource(
body={},
assets=[ImageAsset(source_path=self.source_path)],
)

symbol_resolver = IntrinsicsSymbolTable(template=input_template, logical_id_translator={})
resolver = IntrinsicResolver(template=input_template, symbol_resolver=symbol_resolver)
processed_template = resolver.resolve_template()
self.assertEqual(processed_template["Resources"]["LambdaFunction"], output_resource)

0 comments on commit 73673b4

Please sign in to comment.