/// <summary>
        /// If the lambda has embedded code, extract it to a file.
        /// </summary>
        private void ExtractZipFileCode()
        {
            var templateResource = this.TerraformSettings.Resources.First(
                r => r.LogicalResourceId == this.ImportSettings.Resource.LogicalId).TemplateResource;

            var zipFile = templateResource.GetResourcePropertyValue(TerraformExporterConstants.LambdaZipFile);

            if (zipFile == null)
            {
                // No embedded script
                return;
            }

            var runtimeObject = templateResource.GetResourcePropertyValue("Runtime");

            if (runtimeObject == null)
            {
                // No runtime specified
                return;
            }

            var traits = LambdaTraits.FromRuntime(runtimeObject.ToString());

            var dirName = Path.Combine(this.TerraformSettings.ModuleDirectory, "lambda", this.ImportSettings.Resource.LogicalId);

            Directory.CreateDirectory(dirName);
            var fileName = Path.Combine(dirName, $"index{traits.ScriptFileExtension}");

            this.ImportSettings.Logger.LogInformation($"Extracting inline function code to {fileName}");

            File.WriteAllText(
                fileName,
                zipFile.ToString(),
                new UTF8Encoding(false, false));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Resolves the lambda ZipFile code.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="runtime">The runtime.</param>
        /// <param name="cloudFormationResource">The cloud formation resource.</param>
        /// <param name="attributes">The attributes.</param>
        /// <param name="mapping">The mapping.</param>
        /// <param name="inputs">The list of input variables and data sources.</param>
        private static void ResolveLambdaZipCode(
            TextWriter writer,
            string runtime,
            ITemplateObject cloudFormationResource,
            JObject attributes,
            ResourceMapping mapping,
            IList <InputVariable> inputs)
        {
            var traits = LambdaTraits.FromRuntime(runtime);

            var dirName  = Path.Combine("lambda", cloudFormationResource.Name);
            var fileName = Path.Combine(dirName, $"index{traits.ScriptFileExtension}");
            var zipName  = Path.Combine(dirName, $"{cloudFormationResource.Name}_deployment_package.zip");

            // Create zipper resource
            var zipperResource = $"{cloudFormationResource.Name}_deployment_package";

            var archiveDataSource = new DataSourceInput(
                "archive_file",
                zipperResource,
                new Dictionary <string, string>
            {
                { "type", "zip" },
                { "source_file", fileName.Replace("\\", "/") },
                { "output_path", zipName.Replace("\\", "/") }
            });

            inputs.Add(archiveDataSource);

            // Fix up state file.
            // Find handler function
            var script = File.ReadAllText(fileName);
            var m      = traits.HandlerRegex.Match(script);

            if (m.Success)
            {
                UpdatePropertyValue(
                    "handler",
                    cloudFormationResource.Template,
                    attributes,
                    mapping,
                    inputs,
                    $"index.{m.Groups["handler"].Value}");
            }

            UpdatePropertyValue(
                "filename",
                cloudFormationResource.Template,
                attributes,
                mapping,
                inputs,
                new DataSourceReference(
                    "archive_file",
                    $"{cloudFormationResource.Name}_deployment_package",
                    "output_path",
                    false));

            UpdatePropertyValue(
                "source_code_hash",
                cloudFormationResource.Template,
                attributes,
                mapping,
                inputs,
                new DataSourceReference(
                    "archive_file",
                    $"{cloudFormationResource.Name}_deployment_package",
                    "output_base64sha256",
                    false));
        }