aws / aws-extensions-for-dotnet-cli

Extensions to the dotnet CLI to simplify the process of building and publishing .NET Core applications to AWS services
Apache License 2.0
369 stars 86 forks source link

Added ability to use DockerBuildArgs in Amazon.Lambda.Tools serverless template. #219

Closed ashishdhingra closed 2 years ago

ashishdhingra commented 2 years ago

Issue #, if available: #201

Description of changes: Added ability to use DockerBuildArgs in Amazon.Lambda.Tools serverless template. This change required implementation for both JSON and YAML template parsers.

Testing: Tested (in debug mode) using the following templates for sample https://github.com/aws/aws-extensions-for-dotnet-cli/tree/master/testapps/ImageBasedProjects/ServerlessTemplateExamples: JSON template:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Transform": "AWS::Serverless-2016-10-31",
  "Description": "An AWS Serverless Application.",
  "Resources": {

    "UseDockerMetadataFunction": {
      "Type": "AWS::Serverless::Function",
      "Properties": {
        "MemorySize": 256,
        "Timeout": 30,
        "Policies": [
          "AWSLambdaBasicExecutionRole"
        ],
        "PackageType" : "Image",
        "ImageConfig" : {
            "Command" : "UseDockerMetadata::UseDockerMetadata.Function::FunctionHandler"
        }
      },
      "Metadata": {
        "Dockerfile": "Dockerfile.custom",
        "DockerContext": "./UseDockerMetadata",
        "DockerTag": "aws-extensions-tests:usedockermetadata",
        "DockerBuildArgs": {
          "PROJECT_PATH": "/src/path-to/project",
          "PROJECT_FILE": "project.csproj"
        }
      }
    }
  },

  "Outputs": {
  }
}

YAML template:

AWSTemplateFormatVersion: 2010-09-09
Transform: 'AWS::Serverless-2016-10-31'
Description: An AWS Serverless Application.
Resources:
  UseDockerMetadataFunction:
    Type: 'AWS::Serverless::Function'
    Properties:
      MemorySize: 256
      Timeout: 30
      Policies:
        - AWSLambdaBasicExecutionRole
      PackageType: Image
      ImageConfig:
        Command: 'UseDockerMetadata::UseDockerMetadata.Function::FunctionHandler'
    Metadata:
      Dockerfile: Dockerfile.custom
      DockerContext: ./UseDockerMetadata
      DockerTag: 'aws-extensions-tests:usedockermetadata'
      DockerBuildArgs:
        PROJECT_PATH: /src/path-to/project
        PROJECT_FILE: project.csproj
Outputs: {}

Output:

Amazon Lambda Tools for .NET Core applications (5.4.1)
Project Home: https://github.com/aws/aws-extensions-for-dotnet-cli, https://github.com/aws/aws-lambda-dotnet

Enter CloudFormation Stack Name: (CloudFormation stack name for an AWS Serverless application)
teststack
Enter S3 Bucket: (S3 bucket to upload the build output)
mappingNode.Children
Warning: Unable to determine region for bucket mappingNode.Children, assuming bucket is in correct region: The specified bucket does not exist
Processing CloudFormation resource UseDockerMetadataFunction
Initiate packaging of ./UseDockerMetadata for resource UseDockerMetadataFunction
Building Docker image for D:\source\repros\ServerlessTemplateExamples\./UseDockerMetadata
Inspecting Dockerfile to figure how to build project and docker image
Executing docker build
... invoking 'docker build', working folder 'D:\source\repros\ServerlessTemplateExamples\./UseDockerMetadata, docker file D:\source\repros\ServerlessTemplateExamples\./UseDockerMetadata\Dockerfile.custom, image name aws-extensions-tests:usedockermetadata'
... docker build  -f "D:\source\repros\ServerlessTemplateExamples\./UseDockerMetadata\Dockerfile.custom" -t aws-extensions-tests:usedockermetadata --build-arg PROJECT_PATH=/src/path-to/project --build-arg PROJECT_FILE=project.csproj .
... docker build: Sending build context to Docker daemon  228.9kB
... docker build: Step 1/3 : FROM public.ecr.aws/lambda/dotnet:5.0
... docker build: 5.0: Pulling from lambda/dotnet
... docker build: image operating system "linux" cannot be used on this platform
Error executing "docker build"

NOTE: The deployment fails due to incompatibility of Linux image while testing on Windows based environment, which is fine. The correct --build-arg(s) are passed to docker build call.

We need to add version bump commits in dev branch before actual release of queued changes.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

ashishdhingra commented 2 years ago

Change looks good but we need some test coverage.

Done. Please review.