in src/Amazon.Lambda.Tools/TemplateProcessor/TemplateProcessorManager.cs [239:332]
private async Task<UpdateResourceResults> PackageDotnetProjectAsync(IUpdateResourceField field, string location, string[] args)
{
if (field.Resource.UploadType == CodeUploadType.Zip)
{
var command = new Commands.PackageCommand(this.Logger, location, args);
command.LambdaClient = this.OriginatingCommand?.LambdaClient;
command.S3Client = this.OriginatingCommand?.S3Client;
command.IAMClient = this.OriginatingCommand?.IAMClient;
command.CloudFormationClient = this.OriginatingCommand?.CloudFormationClient;
command.DisableRegionAndCredentialsCheck = true;
var outputPackage = GenerateOutputZipFilename(field);
command.OutputPackageFileName = outputPackage;
command.TargetFramework =
LambdaUtilities.DetermineTargetFrameworkFromLambdaRuntime(field.Resource.LambdaRuntime, location, null);
command.Architecture = field.Resource.LambdaArchitecture;
command.LayerVersionArns = field.Resource.LambdaLayers;
// If the project is in the same directory as the CloudFormation template then use any parameters
// that were specified on the command to build the project.
if (IsCurrentDirectory(field.GetLocalPath()))
{
if (!string.IsNullOrEmpty(this.DefaultOptions.TargetFramework))
command.TargetFramework = this.DefaultOptions.TargetFramework;
command.Configuration = this.DefaultOptions.Configuration;
command.DisableVersionCheck = this.DefaultOptions.DisableVersionCheck;
command.MSBuildParameters = this.DefaultOptions.MSBuildParameters;
}
if (!await command.ExecuteAsync())
{
var message = $"Error packaging up project in {location} for CloudFormation resource {field.Resource.Name}";
if (command.LastToolsException != null)
message += $": {command.LastToolsException.Message}";
throw new LambdaToolsException(message, ToolsException.CommonErrorCode.DotnetPublishFailed, command.LastToolsException);
}
var results = new UpdateResourceResults() { ZipArchivePath = outputPackage };
if (!string.IsNullOrEmpty(command.NewDotnetSharedStoreValue))
{
results.DotnetShareStoreEnv = command.NewDotnetSharedStoreValue;
}
return results;
}
else if (field.Resource.UploadType == CodeUploadType.Image)
{
this.Logger.WriteLine($"Building Docker image for {location}");
var pushCommand = new PushDockerImageCommand(Logger, location, args);
pushCommand.ECRClient = OriginatingCommand.ECRClient;
pushCommand.IAMClient = OriginatingCommand.IAMClient;
pushCommand.DisableInteractive = true;
pushCommand.PushDockerImageProperties.DockerFile = field.GetMetadataDockerfile();
pushCommand.PushDockerImageProperties.DockerImageTag = field.GetMetadataDockerTag();
pushCommand.ImageTagUniqueSeed = field.Resource.Name;
pushCommand.Architecture = field.Resource.LambdaArchitecture;
// Refer https://docs.docker.com/engine/reference/commandline/build/#set-build-time-variables---build-arg
Dictionary<string, string> dockerBuildArgs = field.GetMetadataDockerBuildArgs();
if (dockerBuildArgs != null && dockerBuildArgs.Count > 0)
{
StringBuilder buildArgs = new StringBuilder();
foreach (var keyValuePair in dockerBuildArgs)
{
if (keyValuePair.Value != null)
{
buildArgs.Append($"--build-arg {keyValuePair.Key}={keyValuePair.Value} ");
}
else
{
// --build-arg flag could be used without a value, in which case the value from the local environment will be propagated into the Docker container.
buildArgs.Append($"--build-arg {keyValuePair.Key} ");
}
}
pushCommand.PushDockerImageProperties.DockerBuildOptions = buildArgs.ToString().TrimEnd();
}
await pushCommand.PushImageAsync();
if (pushCommand.LastToolsException != null)
throw pushCommand.LastToolsException;
return new UpdateResourceResults { ImageUri = pushCommand.PushedImageUri };
}
else
{
throw new LambdaToolsException($"Unknown upload type for packaging: {field.Resource.UploadType}", LambdaToolsException.LambdaErrorCode.ServerlessTemplateParseError);
}
}