sam-publish


Namesam-publish JSON
Version 0.2.1 PyPI version JSON
download
home_pagehttps://github.com/peterjdavis/sam-publish
SummaryA tool for formatting AWS Serverless Application Model (SAM) templates to CloudFormation for publishing
upload_time2023-04-27 17:11:32
maintainer
docs_urlNone
authorPete Davis
requires_python!=4.0,<=4.0,>=3.7
license
keywords "sam" "cloudformation" "serverless application model"
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Overview
If you author an [AWS Serverless Application Model (SAM)](https://aws.amazon.com/serverless/sam/) template you may wish to publish this as an [AWS CloudFormation](https://docs.aws.amazon.com/cloudformation/index.html) template to allow the user to deploy the solution from the console and remove the need for the user to install the [AWS SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html).

Much of this can be achieved by using commands such as `sam package` to package your template and upload the assets to S3 and [aws-sam-translator](https://pypi.org/project/aws-sam-translator/) to transform the SAM template into a AWS CloudFormation template.  sam-publish allows you to further transform your CloudFormation template in three ways:
* Inlining of [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) functions into the CloudFormation template to allow the user to the see the functions in the main template
* Control of the buckets where the assets are stored e.g. [AWS Step Functions](https://aws.amazon.com/step-functions/) and [Lambda Layers](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html).  The can be useful if you would like to deploy the assets to a separate AWS account which may have publicly accessible buckets available specifically for sharing assets with users.
* Removes the metadata and tags that are added to resources when converted using [aws-sam-translator](https://pypi.org/project/aws-sam-translator/)

# Command Line Arguments

  `--working-folder WORKING_FOLDER` - Working folder for the input and output files.  Normally a local temp folder.
  
  `--cfn-input-template CFN_INPUT_TEMPLATE` - Name of JSON template to transform [default: template.json].  Normally the output from `sam package` command
   
   `--cfn-output-template CFN_OUTPUT_TEMPLATE` - Name of YAML template to output [default: template.yaml].

  `--target-asset-folder TARGET_ASSET_FOLDER` - Local folder the assets should be stored [default: ./Assets/].

  `--lambda-folder LAMBDA_FOLDER` - Location the lambda assets should be stored, this is appended to the target-asset-folder [default: lambda].

  `--layer-folder LAYER_FOLDER` - Location the layer assets should be stored, this is appended to the target-asset-folder [default: layer].

  `--statemachine-folder STATEMACHINE_FOLDER` - Location the statemachine assets should be stored, this is appended to the target-asset-folder [default: statemachine].

  `--target-asset-bucket TARGET_ASSET_BUCKET` - Bucket the assets will be stored in.  This is used update the references in the CloudFormation template.  The assets are not actually copied to this bucket.  Typically this will be done using an `aws s3 sync` command

  `--target-prefix TARGET_PREFIX` - Prefix that should be applied to the updated references in the CloudFormation template if the assets are not going to uploaded to the root [default: ''].

  `--move-assets` - Should references to the assets in the CloudFormation template be updated to a different bucket [default: False]

  `--tidy-tags-metadata` - Should SAM tags and metadata be tidied up [Default: True]?

  `--add-layout-gaps` - Should a new line be added between each resource for readability [Default: True]?

  `--debug` - Enables debug logging [Default: False]

  `--verbose` - Enables verbose logging [Default: True]

# Example uses

Assuming that you have a SAM Template in the current folder e.g. https://github.com/peterjdavis/sam-publish/blob/main/samples/sam-template.yaml then the following commands could be used to transform this to the CloudFormation template shown at https://github.com/peterjdavis/sam-publish/blob/main/samples/cfn-template.yaml
```bash
#!/bin/bash

# Create a virtual environment
python3 -m venv .venv
source .venv/bin/activate

# Install sam-publish
pip3 install sam-publish

# Get some environment variables
AWSAccount=$(aws sts get-caller-identity --query Account --output text)
AWSRegion=$(aws configure get region)
export tmpCFNDir=$(mktemp -d)

# Build the SAM project
sam build -t sam-template.yaml

# Check to make sure the bucket for the SAM assets exists
if aws s3api head-bucket --bucket sam-${AWSAccount}-${AWSRegion} 2>/dev/null; \
    then echo Bucket sam-${AWSAccount}-${AWSRegion} exists; \
    else echo Creating bucket sam-${AWSAccount}-${AWSRegion} && \
        aws s3 mb s3://sam-${AWSAccount}-${AWSRegion} --region ${AWSRegion} ; \
    fi

# Package the SAM template so the assets are available in the s3 bucket and teh updated template is available
sam package -t sam-template.yaml \
            --output-template-file ${tmpCFNDir}/sam-template.tmp.yaml \
            --s3-bucket sam-${AWSAccount}-${AWSRegion} 

# Update the CloudFormation tempalte so lambda's with an InlineSAMFunction: true metadata tag are inlined
# assets are referenced from a parameter call AssetBucket and the layer and lambda are referenced from a default prefix
sam-publish \
    --working-folder ${tmpCFNDir} \
    --cfn-input-template ${tmpCFNDir}/sam-template.tmp.yaml \
    --cfn-output-template cfn-template.yaml \
    --target-asset-folder assets/cfn \
    --target-asset-bucket AssetBucket \
    --move-assets \
    --verbose

# Tidy up the temporary folder
rm -rf ${tmpCFNDir}
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/peterjdavis/sam-publish",
    "name": "sam-publish",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "!=4.0,<=4.0,>=3.7",
    "maintainer_email": "",
    "keywords": "\"SAM\",\"CloudFormation\",\"Serverless Application Model\"",
    "author": "Pete Davis",
    "author_email": "pete@peterjdavis.info",
    "download_url": "https://files.pythonhosted.org/packages/93/25/8aa16e2a4622ac5d0744bfa5ce28cc9ca25f53bb7af6cc26e1e2683cc2ce/sam-publish-0.2.1.tar.gz",
    "platform": null,
    "description": "# Overview\nIf you author an [AWS Serverless Application Model (SAM)](https://aws.amazon.com/serverless/sam/) template you may wish to publish this as an [AWS CloudFormation](https://docs.aws.amazon.com/cloudformation/index.html) template to allow the user to deploy the solution from the console and remove the need for the user to install the [AWS SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html).\n\nMuch of this can be achieved by using commands such as `sam package` to package your template and upload the assets to S3 and [aws-sam-translator](https://pypi.org/project/aws-sam-translator/) to transform the SAM template into a AWS CloudFormation template.  sam-publish allows you to further transform your CloudFormation template in three ways:\n* Inlining of [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) functions into the CloudFormation template to allow the user to the see the functions in the main template\n* Control of the buckets where the assets are stored e.g. [AWS Step Functions](https://aws.amazon.com/step-functions/) and [Lambda Layers](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html).  The can be useful if you would like to deploy the assets to a separate AWS account which may have publicly accessible buckets available specifically for sharing assets with users.\n* Removes the metadata and tags that are added to resources when converted using [aws-sam-translator](https://pypi.org/project/aws-sam-translator/)\n\n# Command Line Arguments\n\n  `--working-folder WORKING_FOLDER` - Working folder for the input and output files.  Normally a local temp folder.\n  \n  `--cfn-input-template CFN_INPUT_TEMPLATE` - Name of JSON template to transform [default: template.json].  Normally the output from `sam package` command\n   \n   `--cfn-output-template CFN_OUTPUT_TEMPLATE` - Name of YAML template to output [default: template.yaml].\n\n  `--target-asset-folder TARGET_ASSET_FOLDER` - Local folder the assets should be stored [default: ./Assets/].\n\n  `--lambda-folder LAMBDA_FOLDER` - Location the lambda assets should be stored, this is appended to the target-asset-folder [default: lambda].\n\n  `--layer-folder LAYER_FOLDER` - Location the layer assets should be stored, this is appended to the target-asset-folder [default: layer].\n\n  `--statemachine-folder STATEMACHINE_FOLDER` - Location the statemachine assets should be stored, this is appended to the target-asset-folder [default: statemachine].\n\n  `--target-asset-bucket TARGET_ASSET_BUCKET` - Bucket the assets will be stored in.  This is used update the references in the CloudFormation template.  The assets are not actually copied to this bucket.  Typically this will be done using an `aws s3 sync` command\n\n  `--target-prefix TARGET_PREFIX` - Prefix that should be applied to the updated references in the CloudFormation template if the assets are not going to uploaded to the root [default: ''].\n\n  `--move-assets` - Should references to the assets in the CloudFormation template be updated to a different bucket [default: False]\n\n  `--tidy-tags-metadata` - Should SAM tags and metadata be tidied up [Default: True]?\n\n  `--add-layout-gaps` - Should a new line be added between each resource for readability [Default: True]?\n\n  `--debug` - Enables debug logging [Default: False]\n\n  `--verbose` - Enables verbose logging [Default: True]\n\n# Example uses\n\nAssuming that you have a SAM Template in the current folder e.g. https://github.com/peterjdavis/sam-publish/blob/main/samples/sam-template.yaml then the following commands could be used to transform this to the CloudFormation template shown at https://github.com/peterjdavis/sam-publish/blob/main/samples/cfn-template.yaml\n```bash\n#!/bin/bash\n\n# Create a virtual environment\npython3 -m venv .venv\nsource .venv/bin/activate\n\n# Install sam-publish\npip3 install sam-publish\n\n# Get some environment variables\nAWSAccount=$(aws sts get-caller-identity --query Account --output text)\nAWSRegion=$(aws configure get region)\nexport tmpCFNDir=$(mktemp -d)\n\n# Build the SAM project\nsam build -t sam-template.yaml\n\n# Check to make sure the bucket for the SAM assets exists\nif aws s3api head-bucket --bucket sam-${AWSAccount}-${AWSRegion} 2>/dev/null; \\\n    then echo Bucket sam-${AWSAccount}-${AWSRegion} exists; \\\n    else echo Creating bucket sam-${AWSAccount}-${AWSRegion} && \\\n        aws s3 mb s3://sam-${AWSAccount}-${AWSRegion} --region ${AWSRegion} ; \\\n    fi\n\n# Package the SAM template so the assets are available in the s3 bucket and teh updated template is available\nsam package -t sam-template.yaml \\\n            --output-template-file ${tmpCFNDir}/sam-template.tmp.yaml \\\n            --s3-bucket sam-${AWSAccount}-${AWSRegion} \n\n# Update the CloudFormation tempalte so lambda's with an InlineSAMFunction: true metadata tag are inlined\n# assets are referenced from a parameter call AssetBucket and the layer and lambda are referenced from a default prefix\nsam-publish \\\n    --working-folder ${tmpCFNDir} \\\n    --cfn-input-template ${tmpCFNDir}/sam-template.tmp.yaml \\\n    --cfn-output-template cfn-template.yaml \\\n    --target-asset-folder assets/cfn \\\n    --target-asset-bucket AssetBucket \\\n    --move-assets \\\n    --verbose\n\n# Tidy up the temporary folder\nrm -rf ${tmpCFNDir}\n```\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "A tool for formatting AWS Serverless Application Model (SAM) templates to CloudFormation for publishing",
    "version": "0.2.1",
    "split_keywords": [
        "\"sam\"",
        "\"cloudformation\"",
        "\"serverless application model\""
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "dc22d8e7cce9a351dc4c3c39ac847f6cabd2682cedddbd8822b7749f892e7a52",
                "md5": "0949446f9557df40f56e82bce0fa006f",
                "sha256": "8e2351495563fcaad9d75379da803dd788a8a6628ab53dbad81429cec8030a38"
            },
            "downloads": -1,
            "filename": "sam_publish-0.2.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0949446f9557df40f56e82bce0fa006f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "!=4.0,<=4.0,>=3.7",
            "size": 10966,
            "upload_time": "2023-04-27T17:11:30",
            "upload_time_iso_8601": "2023-04-27T17:11:30.278665Z",
            "url": "https://files.pythonhosted.org/packages/dc/22/d8e7cce9a351dc4c3c39ac847f6cabd2682cedddbd8822b7749f892e7a52/sam_publish-0.2.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "93258aa16e2a4622ac5d0744bfa5ce28cc9ca25f53bb7af6cc26e1e2683cc2ce",
                "md5": "339afa3fe97d017d3effd037a0e200a0",
                "sha256": "d40484f0e970c1f8a8b5786b86da7d5f506a00c970d6d3c610fbc04a0d43f449"
            },
            "downloads": -1,
            "filename": "sam-publish-0.2.1.tar.gz",
            "has_sig": false,
            "md5_digest": "339afa3fe97d017d3effd037a0e200a0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "!=4.0,<=4.0,>=3.7",
            "size": 10749,
            "upload_time": "2023-04-27T17:11:32",
            "upload_time_iso_8601": "2023-04-27T17:11:32.300095Z",
            "url": "https://files.pythonhosted.org/packages/93/25/8aa16e2a4622ac5d0744bfa5ce28cc9ca25f53bb7af6cc26e1e2683cc2ce/sam-publish-0.2.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-04-27 17:11:32",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "peterjdavis",
    "github_project": "sam-publish",
    "lcname": "sam-publish"
}
        
Elapsed time: 0.06215s