Skip to content
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

fix: sam validate with local image #2892

Merged
merged 6 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion samcli/commands/validate/lib/sam_template_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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={})
Expand Down Expand Up @@ -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):
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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