

Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.

# So werden lokale Dateien bei der Bereitstellung AWS SAM hochgeladen
<a name="deploy-upload-local-files"></a>

Wenn Sie Ihre Anwendung auf dem bereitstellen AWS Cloud, AWS CloudFormation müssen Ihre lokalen Dateien zunächst in einen zugänglichen AWS Service wie Amazon Simple Storage Service (Amazon S3) hochgeladen werden. Dies schließt lokale Dateien ein, auf die Ihre AWS SAM Vorlage verweist. Um diese Anforderung zu erfüllen, AWS SAMCLI geht der wie folgt vor, wenn Sie den `sam package` Befehl `sam deploy` oder verwenden:

1. Lädt Ihre lokalen Dateien automatisch in einen AWS Dienst hoch, auf den zugegriffen werden kann.

1. Aktualisiert Ihre Anwendungsvorlage automatisch, sodass sie auf den neuen Dateipfad verweist.

**Topics**
+ [Demo: Verwenden Sie AWS SAMCLI den Lambda-Funktionscode zum Hochladen](#deploy-upload-local-files-demo)
+ [Unterstützte Anwendungsfälle](#deploy-upload-local-files-use)
+ [Weitere Informationen](#deploy-upload-local-files-learn)

## Demo: Verwenden Sie AWS SAMCLI den Lambda-Funktionscode zum Hochladen
<a name="deploy-upload-local-files-demo"></a>

In dieser Demo initialisieren wir die Hello World-Beispielanwendung mit einem Pakettyp .zip für unsere Lambda-Funktion. Wir verwenden den AWS SAMCLI, um unseren Lambda-Funktionscode automatisch auf Amazon S3 hochzuladen und auf seinen neuen Pfad in unserer Anwendungsvorlage zu verweisen.

Zuerst starten wir, `sam init` um unsere Hello World-Anwendung zu initialisieren.

```
$ sam init
...
Which template source would you like to use?
        1 - AWS Quick Start Templates
        2 - Custom Template Location
Choice: 1

Choose an AWS Quick Start application template
        1 - Hello World Example
        2 - Multi-step workflow
        ...
Template: 1

Use the most popular runtime and package type? (Python and zip) [y/N]: y

Would you like to enable X-Ray tracing on the function(s) in your application?  [y/N]: ENTER

Would you like to enable monitoring using CloudWatch Application Insights?
For more info, please view https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch-application-insights.html [y/N]: ENTER

Project name [sam-app]: demo

    -----------------------
    Generating application:
    -----------------------
    Name: demo
    Runtime: python3.9
    Architectures: x86_64
    Dependency Manager: pip
    Application Template: hello-world
    Output Directory: .
    Configuration file: demo/samconfig.toml
    
...
```

Unser Lambda-Funktionscode ist im `hello_world` Unterverzeichnis unseres Projekts organisiert.

```
demo
├── README.md
├── hello_world
│   ├── __init__.py
│   ├── app.py
│   └── requirements.txt
├── template.yaml
└── tests
```

In unserer AWS SAM Vorlage verweisen wir mithilfe der `CodeUri` Eigenschaft auf den lokalen Pfad zu unserem Lambda-Funktionscode.

```
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
...
Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.9
      ...
```

Als Nächstes erstellen wir unsere `sam build` Anwendung und bereiten sie auf die Bereitstellung vor.

```
$ sam build
Starting Build use cache
Manifest file is changed (new hash: 3298f13049d19cffaa37ca931dd4d421) or dependency folder (.aws-sam/deps/7896875f-9bcc-4350-8adb-2c1d543627a1) is missing for (HelloWorldFunction), downloading dependencies and copying/building source
Building codeuri: /Users/.../demo/hello_world runtime: python3.9 metadata: {} architecture: x86_64 functions: HelloWorldFunction
Running PythonPipBuilder:CleanUp
Running PythonPipBuilder:ResolveDependencies
Running PythonPipBuilder:CopySource
Running PythonPipBuilder:CopySource

Build Succeeded

Built Artifacts  : .aws-sam/build
Built Template   : .aws-sam/build/template.yaml
...
```

Als Nächstes starten wir, `sam deploy --guided` um unsere Anwendung bereitzustellen.

```
$ sam deploy --guided

Configuring SAM deploy
======================

        Looking for config file [samconfig.toml] :  Found
        Reading default arguments  :  Success

        Setting default arguments for 'sam deploy'
        =========================================
        Stack Name [demo]: ENTER
        AWS Region [us-west-2]: ENTER
        #Shows you resources changes to be deployed and require a 'Y' to initiate deploy
        Confirm changes before deploy [Y/n]: n
        #SAM needs permission to be able to create roles to connect to the resources in your template
        Allow SAM CLI IAM role creation [Y/n]: ENTER
        #Preserves the state of previously provisioned resources when an operation fails
        Disable rollback [y/N]: ENTER
        HelloWorldFunction may not have authorization defined, Is this okay? [y/N]: y
        Save arguments to configuration file [Y/n]: ENTER
        SAM configuration file [samconfig.toml]: ENTER
        SAM configuration environment [default]: ENTER

        Looking for resources needed for deployment:
        ...
        Saved arguments to config file
        Running 'sam deploy' for future deployments will use the parameters saved above.
        The above parameters can be changed by modifying samconfig.toml
        Learn more about samconfig.toml syntax at 
        https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html

File with same data already exists at demo/da3c598813f1c2151579b73ad788cac8, skipping upload

        Deploying with following values
        ===============================
        Stack name                   : demo
        Region                       : us-west-2
        Confirm changeset            : False
        Disable rollback             : False
        Deployment s3 bucket         : aws-sam-cli-managed-default-samclisam-s3-demo-bucket-1a4x26zbcdkqr
        Capabilities                 : ["CAPABILITY_IAM"]
        Parameter overrides          : {}
        Signing Profiles             : {}

Initiating deployment
=====================
...
Waiting for changeset to be created..
CloudFormation stack changeset
-------------------------------------------------------------------------------------------------
Operation                LogicalResourceId        ResourceType             Replacement            
-------------------------------------------------------------------------------------------------
+ Add                    HelloWorldFunctionHell   AWS::Lambda::Permissio   N/A                    
                         oWorldPermissionProd     n                                               
+ Add                    HelloWorldFunctionRole   AWS::IAM::Role           N/A                    
...                     
-------------------------------------------------------------------------------------------------

Changeset created successfully. arn:aws:cloudformation:us-west-2:012345678910:changeSet/samcli-deploy1680906292/1164338d-72e7-4593-a372-f2b3e67f542f

2023-04-07 12:24:58 - Waiting for stack create/update to complete

CloudFormation events from stack operations (refresh every 5.0 seconds)
-------------------------------------------------------------------------------------------------
ResourceStatus           ResourceType             LogicalResourceId        ResourceStatusReason   
-------------------------------------------------------------------------------------------------
CREATE_IN_PROGRESS       AWS::IAM::Role           HelloWorldFunctionRole   -                      
CREATE_IN_PROGRESS       AWS::IAM::Role           HelloWorldFunctionRole   Resource creation      
                                                                           Initiated              
...                    
-------------------------------------------------------------------------------------------------
CloudFormation outputs from deployed stack
-------------------------------------------------------------------------------------------------
Outputs                                                                                         
-------------------------------------------------------------------------------------------------
Key                 HelloWorldFunctionIamRole                                                   
Description         Implicit IAM Role created for Hello World function                          
Value               arn:aws:iam::012345678910:role/demo-HelloWorldFunctionRole-VQ4CU7UY7S2K     

Key                 HelloWorldApi                                                               
Description         API Gateway endpoint URL for Prod stage for Hello World function            
Value               https://satnon55e9.execute-api.us-west-2.amazonaws.com/Prod/hello/          

Key                 HelloWorldFunction                                                          
Description         Hello World Lambda Function ARN                                             
Value               arn:aws:lambda:us-west-2:012345678910:function:demo-                        
HelloWorldFunction-G14inKTmSQvK                                                                 
-------------------------------------------------------------------------------------------------
Successfully created/updated stack - demo in us-west-2
```

Während der Bereitstellung lädt der AWS SAMCLI automatisch unseren Lambda-Funktionscode auf Amazon S3 hoch und aktualisiert unsere Vorlage. Unsere geänderte Vorlage in der CloudFormation Konsole spiegelt den Amazon S3 S3-Bucket-Pfad wider.

```
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
...
Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: s3://aws-sam-cli-managed-default-samclisam-s3-demo-bucket-1a4x26zbcdkqr/demo/da3c598813f1c2151579b73ad788cac8
      Handler: app.lambda_handler
      ...
```

## Unterstützte Anwendungsfälle
<a name="deploy-upload-local-files-use"></a>

Das AWS SAMCLI kann diesen Vorgang für eine Reihe von Dateitypen, CloudFormation Ressourcentypen und CloudFormation Makros automatisch vereinfachen.

### Dateitypen
<a name="deploy-upload-local-files-use-types"></a>

Anwendungsdateien und Docker Bilder werden unterstützt.

### CloudFormation Ressourcentypen
<a name="deploy-upload-local-files-use-resources"></a>

Im Folgenden finden Sie eine Liste der unterstützten Ressourcentypen und ihrer Eigenschaften:


| Ressource | Eigenschaften | 
| --- | --- | 
| AWS::ApiGateway::RestApi | BodyS3Location | 
| AWS::ApiGatewayV2::Api | BodyS3Location | 
| AWS::AppSync:FunctionConfiguration |  `CodeS3Location` `RequestMappingTemplateS3Location` `ResponseMappingTemplateS3Location`  | 
| AWS::AppSync::GraphQLSchema | DefinitionS3Location | 
| AWS::AppSync::Resolver |  `CodeS3Location` `RequestMappingTemplateS3Location` `ResponseMappingTemplateS3Location`  | 
| AWS::CloudFormation::ModuleVersion | ModulePackage | 
| AWS::CloudFormation::ResourceVersion | SchemaHandlerPackage | 
| AWS::ECR::Repository | RepositoryName | 
| AWS::ElasticBeanstalk::ApplicationVersion | SourceBundle | 
| AWS::Glue::Job | Command.ScriptLocation | 
| AWS::Lambda::Function |  `Code` `Code.ImageUri`  | 
| AWS::Lambda::LayerVersion | Content | 
| AWS::Serverless::Api | DefinitionUri | 
| AWS::Serverless::Function |  `CodeUri` `ImageUri`  | 
| AWS::Serverless::GraphQLApi |  `SchemaUri` `Function.CodeUri` `Resolver.CodeUri`  | 
| AWS::Serverless::HttpApi | DefinitionUri | 
| AWS::Serverless::LayerVersion | ContentUri | 
| AWS::Serverless::StateMachine | DefinitionUri | 
| AWS::StepFunctions::StateMachine | DefinitionS3Location | 

### CloudFormation Makros
<a name="deploy-upload-local-files-use-macros"></a>

Dateien, auf die mit dem `AWS::Include` Transform-Makro verwiesen wird, werden unterstützt.

## Weitere Informationen
<a name="deploy-upload-local-files-learn"></a>

Weitere Informationen zur `AWS::Include` Transformation finden Sie unter [ AWS::Include Transformation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/create-reusable-transform-function-snippets-and-add-to-your-template-with-aws-include-transform.html) im *AWS CloudFormation Benutzerhandbuch*.

Ein Beispiel für die Verwendung der `AWS::Include` Transformation in einer AWS SAM Vorlage finden Sie im [API-Gateway-HTTP-API-zu-SQS-Muster](https://serverlessland.com/patterns/apigw-sqs) auf *Serverless Land*.