

Questa è la nuova *Guida di riferimento ai modelli CloudFormation *. Aggiorna i segnalibri e i link. Per informazioni su come iniziare CloudFormation, consulta la [Guida AWS CloudFormation per l'utente](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html).

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

# Attributo `Condition`
<a name="aws-attribute-condition"></a>

Per creare risorse in CloudFormation base alle condizioni, per prima cosa dichiara la tua condizione nella sezione del `Conditions` modello. Quindi, usa la `Condition` chiave insieme all'ID logico della condizione come attributo della risorsa. CloudFormation crea la risorsa solo quando la condizione risulta vera. Ciò consente di controllare la creazione di risorse in base a criteri o parametri specifici definiti nel modello.

Se non conosci l'utilizzo delle condizioni nei tuoi modelli, ti consigliamo di consultare prima l'argomento sulla [sintassi delle condizioni del CloudFormation modello](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/conditions-section-structure.html) nella Guida per l'*AWS CloudFormation utente*.

**Nota**  
Se non viene creata una risorsa con una condizione, non vengono create nemmeno le risorse che dipendono da tale risorsa, indipendentemente dalle loro condizioni.

## Esempio
<a name="aws-attribute-condition-example"></a>

Il modello seguente contiene una risorsa del bucket Amazon S3 con un attributo `Condition`. Il bucket viene creato solo se la condizione `CreateBucket` corrisponde a `true`.

### JSON
<a name="aws-attribute-condition-example.json"></a>

```
{
   "AWSTemplateFormatVersion" : "2010-09-09",
   "Parameters" : {
      "EnvType" : {
         "Type" : "String",
         "AllowedValues" : ["prod", "dev"],
         "Default" : "dev",
         "Description" : "Environment type"
      }
   },
   "Conditions" : {
      "CreateBucket" : {"Fn::Equals" : [{"Ref" : "EnvType"}, "prod"]}
   },
   "Resources" : {
      "MyBucket" : {
         "Type" : "AWS::S3::Bucket",
         "Condition" : "CreateBucket",
         "Properties" : {
            "BucketName" : {"Fn::Join" : ["-", ["mybucket", {"Ref" : "EnvType"}]]}
         }
      }
   }
}
```

### YAML
<a name="aws-attribute-condition-example.yaml"></a>

```
 1. AWSTemplateFormatVersion: 2010-09-09
 2. Parameters:
 3.   EnvType:
 4.     Type: String
 5.     AllowedValues:
 6.       - prod
 7.       - dev
 8.     Default: dev
 9.     Description: Environment type
10. Conditions:
11.   CreateBucket: !Equals [!Ref EnvType, prod]
12. Resources:
13.   MyBucket:
14.     Type: AWS::S3::Bucket
15.     Condition: CreateBucket
16.     Properties:
17.       BucketName: !Sub mybucket-${EnvType}
```

## Utilizzo di più condizioni
<a name="aws-attribute-condition-multiple"></a>

Puoi combinare più condizioni utilizzando funzioni intrinseche come [`Fn::And`](intrinsic-function-reference-conditions.md#intrinsic-function-reference-conditions-and), [`Fn::Or`](intrinsic-function-reference-conditions.md#intrinsic-function-reference-conditions-or), e [`Fn::Not`](intrinsic-function-reference-conditions.md#intrinsic-function-reference-conditions-not) per creare una logica condizionale più complessa.

### JSON
<a name="aws-attribute-condition-multiple.json"></a>

```
{
   "AWSTemplateFormatVersion" : "2010-09-09",
   "Parameters" : {
      "EnvType" : {
         "Type" : "String",
         "AllowedValues" : ["prod", "test", "dev"],
         "Default" : "dev",
         "Description" : "Environment type"
      },
      "CreateResources" : {
         "Type" : "String",
         "AllowedValues" : ["true", "false"],
         "Default" : "true",
         "Description" : "Create resources flag"
      }
   },
   "Conditions" : {
      "IsProd" : {"Fn::Equals" : [{"Ref" : "EnvType"}, "prod"]},
      "IsTest" : {"Fn::Equals" : [{"Ref" : "EnvType"}, "test"]},
      "CreateResourcesFlag" : {"Fn::Equals" : [{"Ref" : "CreateResources"}, "true"]},
      "CreateProdResources" : {"Fn::And" : [{"Condition" : "IsProd"}, {"Condition" : "CreateResourcesFlag"}]},
      "CreateTestOrDevResources" : {"Fn::And" : [{"Fn::Or" : [{"Condition" : "IsTest"}, {"Fn::Not" : [{"Condition" : "IsProd"}]}]}, {"Condition" : "CreateResourcesFlag"}]}
   },
   "Resources" : {
      "ProdBucket" : {
         "Type" : "AWS::S3::Bucket",
         "Condition" : "CreateProdResources",
         "Properties" : {
            "BucketName" : {"Fn::Join" : ["-", ["prod-bucket", {"Ref" : "AWS::StackName"}]]}
         }
      },
      "TestDevBucket" : {
         "Type" : "AWS::S3::Bucket",
         "Condition" : "CreateTestOrDevResources",
         "Properties" : {
            "BucketName" : {"Fn::Join" : ["-", [{"Ref" : "EnvType"}, "bucket", {"Ref" : "AWS::StackName"}]]}
         }
      }
   }
}
```

### YAML
<a name="aws-attribute-condition-multiple.yaml"></a>

```
 1. AWSTemplateFormatVersion: 2010-09-09
 2. Parameters:
 3.   EnvType:
 4.     Type: String
 5.     AllowedValues:
 6.       - prod
 7.       - test
 8.       - dev
 9.     Default: dev
10.     Description: Environment type
11.   CreateResources:
12.     Type: String
13.     AllowedValues:
14.       - 'true'
15.       - 'false'
16.     Default: 'true'
17.     Description: Create resources flag
18. Conditions:
19.   IsProd: !Equals [!Ref EnvType, prod]
20.   IsTest: !Equals [!Ref EnvType, test]
21.   CreateResourcesFlag: !Equals [!Ref CreateResources, 'true']
22.   CreateProdResources: !And
23.     - !Condition IsProd
24.     - !Condition CreateResourcesFlag
25.   CreateTestOrDevResources: !And
26.     - !Or
27.       - !Condition IsTest
28.       - !Not [!Condition IsProd]
29.     - !Condition CreateResourcesFlag
30. Resources:
31.   ProdBucket:
32.     Type: AWS::S3::Bucket
33.     Condition: CreateProdResources
34.     Properties:
35.       BucketName: !Sub prod-bucket-${AWS::StackName}
36.   TestDevBucket:
37.     Type: AWS::S3::Bucket
38.     Condition: CreateTestOrDevResources
39.     Properties:
40.       BucketName: !Sub ${EnvType}-bucket-${AWS::StackName}
```

## Utilizzo di `AWS::AccountId` nelle condizioni
<a name="aws-attribute-condition-account"></a>

Puoi utilizzare gli pseudo parametri, ad esempio `AWS::AccountId` nelle tue condizioni, per creare risorse in base al Account AWS luogo in cui viene distribuito lo stack. Ciò è utile per le implementazioni con più account o quando è necessario escludere account specifici dalla ricezione di determinate risorse. *Per ulteriori informazioni sugli pseudo parametri, consulta [Ottenere AWS valori utilizzando](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/pseudo-parameter-reference.html) gli pseudo parametri nella Guida per l'utente.AWS CloudFormation *

### JSON
<a name="aws-attribute-condition-account.json"></a>

```
{
   "AWSTemplateFormatVersion" : "2010-09-09",
   "Conditions" : {
      "ExcludeAccount1" : {"Fn::Not" : [{"Fn::Equals" : [{"Ref" : "AWS::AccountId"}, "111111111111"]}]},
      "ExcludeAccount2" : {"Fn::Not" : [{"Fn::Equals" : [{"Ref" : "AWS::AccountId"}, "222222222222"]}]},
      "ExcludeBothAccounts" : {"Fn::And" : [{"Condition" : "ExcludeAccount1"}, {"Condition" : "ExcludeAccount2"}]}
   },
   "Resources" : {
      "StandardBucket" : {
         "Type" : "AWS::S3::Bucket",
         "Properties" : {
            "BucketName" : {"Fn::Join" : ["-", ["standard-bucket", {"Ref" : "AWS::StackName"}]]}
         }
      },
      "RestrictedResource" : {
         "Type" : "AWS::SNS::Topic",
         "Condition" : "ExcludeBothAccounts",
         "Properties" : {
            "TopicName" : {"Fn::Join" : ["-", ["restricted-topic", {"Ref" : "AWS::StackName"}]]}
         }
      }
   }
}
```

### YAML
<a name="aws-attribute-condition-account.yaml"></a>

```
 1. AWSTemplateFormatVersion: 2010-09-09
 2. Conditions:
 3.   ExcludeAccount1: !Not [!Equals [!Ref 'AWS::AccountId', '111111111111']]
 4.   ExcludeAccount2: !Not [!Equals [!Ref 'AWS::AccountId', '222222222222']]
 5.   ExcludeBothAccounts: !And
 6.     - !Condition ExcludeAccount1
 7.     - !Condition ExcludeAccount2
 8. Resources:
 9.   StandardBucket:
10.     Type: AWS::S3::Bucket
11.     Properties:
12.       BucketName: !Sub standard-bucket-${AWS::StackName}
13.   RestrictedResource:
14.     Type: AWS::SNS::Topic
15.     Condition: ExcludeBothAccounts
16.     Properties:
17.       TopicName: !Sub restricted-topic-${AWS::StackName}
```