diff --git a/samcli/commands/validate/lib/sam_template_validator.py b/samcli/commands/validate/lib/sam_template_validator.py index 4e34071b9f..d9de756674 100644 --- a/samcli/commands/validate/lib/sam_template_validator.py +++ b/samcli/commands/validate/lib/sam_template_validator.py @@ -9,7 +9,8 @@ from samtranslator.translator.translator import Translator from boto3.session import Session -from samcli.lib.utils.packagetype import ZIP +from samcli.lib.utils.packagetype import ZIP, IMAGE +from samcli.commands._utils.resources import AWS_SERVERLESS_FUNCTION from samcli.yamlhelper import yaml_dump from .exceptions import InvalidSamDocumentException @@ -63,6 +64,7 @@ def is_valid(self): ) self._replace_local_codeuri() + self._replace_local_image() try: template = sam_translator.translate(sam_template=self.sam_template, parameter_values={}) @@ -120,6 +122,23 @@ def _replace_local_codeuri(self): if "DefinitionUri" in resource_dict: SamTemplateValidator._update_to_s3_uri("DefinitionUri", resource_dict) + def _replace_local_image(self): + """ + Adds fake ImageUri to AWS::Serverless::Functions that reference a local image using Metadata. + This ensures sam validate works without having to package the app or use ImageUri. + """ + resources = self.sam_template.get("Resources", {}) + for _, resource in resources.items(): + resource_type = resource.get("Type") + properties = resource.get("Properties", {}) + + is_image_function = resource_type == AWS_SERVERLESS_FUNCTION and properties.get("PackageType") == IMAGE + is_local_image = resource.get("Metadata", {}).get("Dockerfile") + + if is_image_function and is_local_image: + if "ImageUri" not in properties: + properties["ImageUri"] = "111111111111.dkr.ecr.region.amazonaws.com/repository" + @staticmethod def is_s3_uri(uri): """ diff --git a/tests/functional/commands/validate/lib/models/function_with_image_in_imageuri.yaml b/tests/functional/commands/validate/lib/models/function_with_image_in_imageuri.yaml new file mode 100644 index 0000000000..66195a75e5 --- /dev/null +++ b/tests/functional/commands/validate/lib/models/function_with_image_in_imageuri.yaml @@ -0,0 +1,8 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Resources: + HelloWorldFunction: + Type: AWS::Serverless::Function + Properties: + PackageType: Image + ImageUri: 111111111111.dkr.ecr.region.amazonaws.com/myrepo diff --git a/tests/functional/commands/validate/lib/models/function_with_image_in_metadata.yaml b/tests/functional/commands/validate/lib/models/function_with_image_in_metadata.yaml new file mode 100644 index 0000000000..81bc940308 --- /dev/null +++ b/tests/functional/commands/validate/lib/models/function_with_image_in_metadata.yaml @@ -0,0 +1,11 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Resources: + HelloWorldFunction: + Type: AWS::Serverless::Function + Properties: + PackageType: Image + Metadata: + DockerTag: nodejs14.x-v1 + DockerContext: ./hello-world + Dockerfile: Dockerfile