aws / aws-sam-cli

CLI tool to build, test, debug, and deploy Serverless applications using AWS SAM
https://aws.amazon.com/serverless/sam/
Apache License 2.0
6.5k stars 1.17k forks source link

Bug: SAM crash when using Fn::Transform in non Serverless resource #6204

Closed having-fun-serverless closed 10 months ago

having-fun-serverless commented 10 months ago

Description:

I created and deployed a macro. When using this macro with a non Serverless construct, like DynamoDB, AWS SAM crashes. I'm aware of the issues with intrinsic functions as mentioned here - https://github.com/aws/serverless-application-model/issues/2533 , but I'm using my function in a non Serverless resource.

Steps to reproduce:

I'm using the following template:

AWSTemplateFormatVersion: "2010-09-09"
Transform:
  - AWS::Serverless-2016-10-31
Description: >
  sam-app

  Sample SAM Template for sam-app

Globals:
  Function:
    Timeout: 3

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: "Test"

      InlineCode: |
        import json
        def handler(event, context):
          return {
            "statusCode": 200,
            "body": json.dumps({
                "message": "hello world",
            }),
          }
      Handler: index.handler
      Runtime: python3.11
      Architectures:
        - arm64

  MyNewTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: "MyNewTable"
      KeySchema:
        - AttributeName: key
          KeyType: HASH
      AttributeDefinitions:
        - AttributeName: key
          AttributeType: S
      Fn::Transform: TestMacro

Observed result:

When using aws cloudformation create-stack command to create a stack, it works, however when using sam deploy I'm getting

2023-11-05 11:29:32,368 | Config file location: /home/ubuntu/code/cloudformation-templates/sam-app/samconfig.toml                                                   
2023-11-05 11:29:32,370 | Loading configuration values from [default.['deploy'].parameters] (env.command_name.section) in config file at                            
'/home/ubuntu/code/cloudformation-templates/sam-app/samconfig.toml'...                                                                                              
2023-11-05 11:29:32,372 | Configuration values successfully loaded.                                                                                                 
2023-11-05 11:29:32,372 | Configuration values are: {'stack_name': 'sam-app', 'capabilities': 'CAPABILITY_IAM', 'confirm_changeset': True, 'resolve_s3': True}      
2023-11-05 11:29:32,375 | Using SAM Template at /home/ubuntu/code/cloudformation-templates/sam-app/.aws-sam/build/template.yaml                                     
2023-11-05 11:29:32,400 | Using config file: samconfig.toml, config environment: default                                                                            
2023-11-05 11:29:32,401 | Expand command line arguments to:                                                                                                         
2023-11-05 11:29:32,401 | --template_file=/home/ubuntu/code/cloudformation-templates/sam-app/.aws-sam/build/template.yaml --fail_on_empty_changeset                 
--confirm_changeset --on_failure=ROLLBACK --stack_name=sam-app --resolve_s3 --capabilities=['CAPABILITY_IAM']                                                       

      Managed S3 bucket: aws-sam-cli-managed-default-samclisourcebucket-jlz1unotbrtp
      A different default S3 bucket can be set in samconfig.toml
      Or by specifying --s3-bucket explicitly.
2023-11-05 11:29:33,243 | No Parameters detected in the template                                                                                                    
2023-11-05 11:29:33,260 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the resource logical id as the        
resource id                                                                                                                                                         
2023-11-05 11:29:33,260 | There is no customer defined id or cdk path defined for resource MyNewTable, so we will use the resource logical id as the resource id    
2023-11-05 11:29:33,261 | 0 stacks found in the template                                                                                                            
2023-11-05 11:29:33,262 | No Parameters detected in the template                                                                                                    
2023-11-05 11:29:33,303 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the resource logical id as the        
resource id                                                                                                                                                         
2023-11-05 11:29:33,304 | There is no customer defined id or cdk path defined for resource MyNewTable, so we will use the resource logical id as the resource id    
2023-11-05 11:29:33,329 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the resource logical id as the        
resource id                                                                                                                                                         
2023-11-05 11:29:33,330 | There is no customer defined id or cdk path defined for resource MyNewTable, so we will use the resource logical id as the resource id    
2023-11-05 11:29:33,331 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics                                
2023-11-05 11:29:33,336 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics                                
2023-11-05 11:29:33,337 | Sending Telemetry: {'metrics': [{'commandRun': {'requestId': 'f9320eef-cdf1-437d-8f00-12211a81f8a2', 'installationId':                    
'bc865d07-39c7-4c50-94fa-f6f10520612d', 'sessionId': '4032ab38-b7ac-4f4e-b39a-f6e890eeb35a', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.11.3',     
'samcliVersion': '1.100.0', 'awsProfileProvided': False, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam deploy', 'metricSpecificAttributes':          
{'projectType': 'CFN', 'gitOrigin': None, 'projectName': '593ab2ca51e925b9f6c2f258bc55ed5926cf6d2c78239a685d65907e4ec7edd3', 'initialCommit': None}, 'duration':    
930, 'exitReason': 'AttributeError', 'exitCode': 255}}]}                                                                                                            
2023-11-05 11:29:33,337 | Unable to find Click Context for getting session_id.                                                                                      
2023-11-05 11:29:33,340 | Sending Telemetry: {'metrics': [{'events': {'requestId': 'b19e769e-32b3-4dbf-b2f4-955862bafe6c', 'installationId':                        
'bc865d07-39c7-4c50-94fa-f6f10520612d', 'sessionId': '4032ab38-b7ac-4f4e-b39a-f6e890eeb35a', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.11.3',     
'samcliVersion': '1.100.0', 'commandName': 'sam deploy', 'metricSpecificAttributes': {'events': [{'event_name': 'SamConfigFileExtension', 'event_value': '.toml',   
'thread_id': '7ef77eacbeff44479289aa5780489da1', 'time_stamp': '2023-11-05 11:29:32.368', 'exception_name': None}, {'event_name': 'SamConfigFileExtension',         
'event_value': '.toml', 'thread_id': 'c158d3360ad743f290e9deb62659c761', 'time_stamp': '2023-11-05 11:29:32.398', 'exception_name': None}]}}}]}                     
2023-11-05 11:29:34,031 | HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read timed out. (read timeout=0.1)          

Error: 'str' object has no attribute 'get'
Traceback:
  File "click/core.py", line 1078, in main
  File "click/core.py", line 1688, in invoke
  File "click/core.py", line 1434, in invoke
  File "click/core.py", line 783, in invoke
  File "samcli/cli/cli_config_file.py", line 347, in wrapper
  File "samcli/lib/cli_validation/image_repository_validation.py", line 110, in wrapped
  File "click/decorators.py", line 92, in new_func
  File "click/core.py", line 783, in invoke
  File "samcli/lib/telemetry/metric.py", line 184, in wrapped
  File "samcli/lib/telemetry/metric.py", line 149, in wrapped
  File "samcli/lib/utils/version_checker.py", line 42, in wrapped
  File "samcli/cli/main.py", line 95, in wrapper
  File "samcli/commands/_utils/cdk_support_decorators.py", line 39, in wrapped
  File "samcli/commands/_utils/command_exception_handler.py", line 88, in wrapper_command_exception_handler
  File "samcli/commands/_utils/command_exception_handler.py", line 68, in wrapper_command_exception_handler
  File "samcli/commands/deploy/command.py", line 200, in cli
  File "samcli/commands/deploy/command.py", line 329, in do_cli
  File "samcli/commands/package/package_context.py", line 141, in run
  File "samcli/commands/package/package_context.py", line 163, in _export
  File "samcli/lib/package/artifact_exporter.py", line 276, in export
  File "samcli/lib/package/artifact_exporter.py", line 218, in _export_global_artifacts
  File "samcli/lib/package/artifact_exporter.py", line 218, in _export_global_artifacts
  File "samcli/lib/package/artifact_exporter.py", line 218, in _export_global_artifacts
  File "samcli/lib/package/artifact_exporter.py", line 214, in _export_global_artifacts
  File "samcli/lib/package/packageable_resources.py", line 714, in include_transform_export_handler

Expected result:

Getting a successful deploy like aws cloudformation create-stack cli

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

{ "version": "1.100.0", "system": { "python": "3.11.3", "os": "Linux-6.2.0-1015-aws-x86_64-with-glibc2.35" }, "additional_dependencies": { "docker_engine": "24.0.5", "aws_cdk": "2.99.1 (build b2a895e)", "terraform": "Not available" }, "available_beta_feature_env_vars": [ "SAM_CLI_BETA_FEATURES", "SAM_CLI_BETA_BUILD_PERFORMANCE", "SAM_CLI_BETA_TERRAFORM_SUPPORT", "SAM_CLI_BETA_RUST_CARGO_LAMBDA" ] }

github-actions[bot] commented 10 months ago

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

having-fun-serverless commented 10 months ago

It looks like I was missing the name of the macro, although CW works without specifying the Name property, SAM requires something like

MyNewTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: "MyNewTable"
      KeySchema:
        - AttributeName: key
          KeyType: HASH
      AttributeDefinitions:
        - AttributeName: key
          AttributeType: S
      Fn::Transform: 
          Name: "VariableSubstitution" # <-- here