in src/Amazon.Lambda.Tools/Commands/PublishLayerCommand.cs [88:159]
protected override async Task<bool> PerformActionAsync()
{
var layerName = this.GetStringValueOrDefault(this.LayerName, LambdaDefinedCommandOptions.ARGUMENT_LAYER_NAME, true);
var s3Bucket = this.GetStringValueOrDefault(this.S3Bucket, LambdaDefinedCommandOptions.ARGUMENT_S3_BUCKET, true);
var s3Prefix = this.GetStringValueOrDefault(this.S3Prefix, LambdaDefinedCommandOptions.ARGUMENT_S3_PREFIX, false);
// This command will store 2 file in S3. The object structure will be
// <provided-prefix>/
// <layer-name>-<unique-ticks>/
// packages.zip -- Zip file containing the NuGet packages
// artifact.xml -- Xml file describe the NuGet packages in the layer
if (string.IsNullOrEmpty(s3Prefix))
{
s3Prefix = string.Empty;
}
else if(!s3Prefix.EndsWith("/"))
{
s3Prefix += "/";
}
s3Prefix += $"{layerName}-{DateTime.UtcNow.Ticks}/";
var layerType = this.GetStringValueOrDefault(this.LayerType, LambdaDefinedCommandOptions.ARGUMENT_LAYER_TYPE, true);
CreateLayerZipFileResult createResult;
switch (layerType)
{
case LambdaConstants.LAYER_TYPE_RUNTIME_PACKAGE_STORE:
createResult = await CreateRuntimePackageStoreLayerZipFile(layerName, s3Prefix);
break;
default:
throw new LambdaToolsException($"Unknown layer type {layerType}. Allowed values are: {LambdaConstants.LAYER_TYPE_ALLOWED_VALUES}", LambdaToolsException.LambdaErrorCode.UnknownLayerType);
}
this.Logger.WriteLine($"Uploading layer input zip file to S3");
var s3ZipKey = await UploadFile(createResult.ZipFile, $"{s3Prefix}packages.zip");
var request = new PublishLayerVersionRequest
{
LayerName = layerName,
Description = createResult.Description,
Content = new LayerVersionContentInput
{
S3Bucket = s3Bucket,
S3Key = s3ZipKey
},
CompatibleRuntimes = createResult.CompatibleRuntimes,
LicenseInfo = this.GetStringValueOrDefault(this.LayerLicenseInfo, LambdaDefinedCommandOptions.ARGUMENT_LAYER_LICENSE_INFO, false)
};
var architecture = this.GetStringValueOrDefault(this.Architecture, LambdaDefinedCommandOptions.ARGUMENT_FUNCTION_ARCHITECTURE, false);
if (!string.IsNullOrEmpty(architecture))
{
request.CompatibleArchitectures = new List<string> { architecture };
}
try
{
var publishResponse = await this.LambdaClient.PublishLayerVersionAsync(request);
this.NewLayerArn = publishResponse.LayerArn;
this.NewLayerVersionArn = publishResponse.LayerVersionArn;
this.NewLayerVersionNumber = publishResponse.Version;
}
catch(Exception e)
{
throw new LambdaToolsException($"Error calling the Lambda service to publish the layer: {e.Message}", LambdaToolsException.LambdaErrorCode.LambdaPublishLayerVersion, e);
}
this.Logger?.WriteLine($"Layer publish with arn {this.NewLayerVersionArn}");
return true;
}