

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à.

# Peer con un VPC in un altro Account AWS
<a name="peer-with-vpc-in-another-account"></a>

È possibile effettuare il peering con un Virtual Private Cloud (VPC) in un altro utilizzando. Account AWS [https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-ec2-vpcpeeringconnection.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-ec2-vpcpeeringconnection.html) In questo modo si crea una connessione di rete tra due di essi VPCs che consente di instradare il traffico tra di loro in modo che possano comunicare come se fossero all'interno della stessa rete. Una connessione peering VPC può facilitare l’accesso ai dati e il trasferimento dei dati.

Per stabilire una connessione peering VPC, devi autorizzarne due separate Account AWS all'interno di un unico stack. CloudFormation 

Per ulteriori informazioni sul peering di VPC e le sue limitazioni, consulta la [Guida al peering di Amazon VPC](https://docs.aws.amazon.com/vpc/latest/peering/). 

## Prerequisiti
<a name="peer-with-vpc-in-another-account-prerequisites"></a>

1. Per la connessione peering sono necessari un ID VPC peer, un Account AWS peer ID e [un ruolo di accesso tra account diversi](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_common-scenarios_aws-accounts.html). 
**Nota**  
Questa procedura guidata si riferisce a due account: un account che consente il peering tra più account (l’*account dell’accettante*) e un account che richiede la connessione in peering (l’*account del richiedente*).

1. Per accettare la connessione peering VPC, devi assumere il ruolo di accesso multi-account. La risorsa si comporta nello stesso modo di una risorsa di una connessione peering VPC nello stesso account. Per informazioni su come un amministratore IAM concede le autorizzazioni per assumere il ruolo di multi-account, consulta [Grant a user permissions to switch roles](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_permissions-to-switch.html) nella *Guida per l’utente di IAM*.

## Fase 1: creazione di un VPC e di un ruolo tra più account
<a name="step-1-create-vpc-and-cross-account-role"></a>

In questa fase vengono creati il VPC e il ruolo nell’*account dell’accettante*.

**Creazione di un VPC e di un ruolo di accesso multi-account**

1. [Accedi e apri la console all'indirizzo Console di gestione AWS /cloudformazione. CloudFormation https://console.aws.amazon.com](https://console.aws.amazon.com/cloudformation/)

1. Nella pagina **Stack** scegli **Crea stack** in alto a destra, quindi seleziona **Con nuove risorse (standard)**.

1. Per **Prerequisito: prepara modello**, scegli **Scegli un modello esistente**, quindi **Carica un file modello**, **Scegli file**.

1. Apri un editor di testo sul tuo computer locale e aggiungi uno dei modelli seguenti. Salva il file e torna alla console per selezionarlo come file modello.  
**Example JSON**  

   ```
   {
     "AWSTemplateFormatVersion": "2010-09-09",
     "Description": "Create a VPC and an assumable role for cross account VPC peering.",
     "Parameters": {
       "PeerRequesterAccountId": {
         "Type": "String"
       }
     },
     "Resources": {
       "vpc": {
         "Type": "AWS::EC2::VPC",
         "Properties": {
           "CidrBlock": "10.1.0.0/16",
           "EnableDnsSupport": false,
           "EnableDnsHostnames": false,
           "InstanceTenancy": "default"
         }
       },
       "peerRole": {
         "Type": "AWS::IAM::Role",
         "Properties": {
           "AssumeRolePolicyDocument": {
             "Statement": [
               {
                 "Principal": {
                   "AWS": {
                     "Ref": "PeerRequesterAccountId"
                   }
                 },
                 "Action": [
                   "sts:AssumeRole"
                 ],
                 "Effect": "Allow"
               }
             ]
           },
           "Path": "/",
           "Policies": [
             {
               "PolicyName": "root",
               "PolicyDocument": {
                 "Version": "2012-10-17",		 	 	 
                 "Statement": [
                   {
                     "Effect": "Allow",
                     "Action": "ec2:AcceptVpcPeeringConnection",
                     "Resource": "*"
                   }
                 ]
               }
             }
           ]
         }
       }
     },
     "Outputs": {
       "VPCId": {
         "Value": {
           "Ref": "vpc"
         }
       },
       "RoleARN": {
         "Value": {
           "Fn::GetAtt": [
             "peerRole",
             "Arn"
           ]
         }
       }
     }
   }
   ```  
**Example YAML**  

   ```
   AWSTemplateFormatVersion: 2010-09-09
   Description: Create a VPC and an assumable role for cross account VPC peering.
   Parameters:
     PeerRequesterAccountId:
       Type: String
   Resources:
     vpc:
       Type: AWS::EC2::VPC
       Properties:
         CidrBlock: 10.1.0.0/16
         EnableDnsSupport: false
         EnableDnsHostnames: false
         InstanceTenancy: default
     peerRole:
       Type: AWS::IAM::Role
       Properties:
         AssumeRolePolicyDocument:
           Statement:
             - Principal:
                 AWS: !Ref PeerRequesterAccountId
               Action:
                 - 'sts:AssumeRole'
               Effect: Allow
         Path: /
         Policies:
           - PolicyName: root
             PolicyDocument:
               Version: 2012-10-17 		 	 	 
               Statement:
                 - Effect: Allow
                   Action: 'ec2:AcceptVpcPeeringConnection'
                   Resource: '*'
   Outputs:
     VPCId:
       Value: !Ref vpc
     RoleARN:
       Value: !GetAtt 
         - peerRole
         - Arn
   ```

1. Scegli **Next (Successivo)**.

1. Assegna un nome allo stack (ad esempio,**VPC-owner**), quindi inserisci l' Account AWS ID dell'account *richiedente* nel campo. **PeerRequesterAccountId**

1. Accettare i valori predefiniti, quindi selezionare **Next (Avanti)**.

1. Scegli **Riconosco che CloudFormation potrebbe creare risorse IAM**, quindi scegli **Create** stack.

## Fase 2: creazione di un modello che include `AWS::EC2::VPCPeeringConnection`
<a name="step-2-create-template-for-vpc-peering-connection-owner"></a>

*Ora che hai creato il VPC e il ruolo interaccount, puoi effettuare il peer con il VPC utilizzandone un altro Account AWS (l'account richiedente).*

**Per creare un modello che includa la risorsa [AWS: :EC2:: Connection VPCPeering](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-ec2-vpcpeeringconnection.html)**

1. Torna alla home page della CloudFormation console.

1. Nella pagina **Stack** scegli **Crea stack** in alto a destra, quindi seleziona **Con nuove risorse (standard)**.

1. Per **Prerequisito: prepara modello**, scegli **Scegli un modello esistente**, quindi **Carica un file modello**, **Scegli file**.

1. Apri un editor di testo sul tuo computer locale e aggiungi uno dei modelli seguenti. Salva il file e torna alla console per selezionarlo come file modello.  
**Example JSON**  

   ```
   {
     "AWSTemplateFormatVersion": "2010-09-09",
     "Description": "Create a VPC and a VPC Peering connection using the PeerRole to accept.",
     "Parameters": {
       "PeerVPCAccountId": {
         "Type": "String"
       },
       "PeerVPCId": {
         "Type": "String"
       },
       "PeerRoleArn": {
         "Type": "String"
       }
     },
     "Resources": {
       "vpc": {
         "Type": "AWS::EC2::VPC",
         "Properties": {
           "CidrBlock": "10.2.0.0/16",
           "EnableDnsSupport": false,
           "EnableDnsHostnames": false,
           "InstanceTenancy": "default"
         }
       },
       "vpcPeeringConnection": {
         "Type": "AWS::EC2::VPCPeeringConnection",
         "Properties": {
           "VpcId": {
             "Ref": "vpc"
           },
           "PeerVpcId": {
             "Ref": "PeerVPCId"
           },
           "PeerOwnerId": {
             "Ref": "PeerVPCAccountId"
           },
           "PeerRoleArn": {
             "Ref": "PeerRoleArn"
           }
         }
       }
     },
     "Outputs": {
       "VPCId": {
         "Value": {
           "Ref": "vpc"
         }
       },
       "VPCPeeringConnectionId": {
         "Value": {
           "Ref": "vpcPeeringConnection"
         }
       }
     }
   }
   ```  
**Example YAML**  

   ```
   AWSTemplateFormatVersion: 2010-09-09
   Description: Create a VPC and a VPC Peering connection using the PeerRole to accept.
   Parameters:
     PeerVPCAccountId:
       Type: String
     PeerVPCId:
       Type: String
     PeerRoleArn:
       Type: String
   Resources:
     vpc:
       Type: AWS::EC2::VPC
       Properties:
         CidrBlock: 10.2.0.0/16
         EnableDnsSupport: false
         EnableDnsHostnames: false
         InstanceTenancy: default
     vpcPeeringConnection:
       Type: AWS::EC2::VPCPeeringConnection
       Properties:
         VpcId: !Ref vpc
         PeerVpcId: !Ref PeerVPCId
         PeerOwnerId: !Ref PeerVPCAccountId
         PeerRoleArn: !Ref PeerRoleArn
   Outputs:
     VPCId:
       Value: !Ref vpc
     VPCPeeringConnectionId:
       Value: !Ref vpcPeeringConnection
   ```

1. Scegli **Next (Successivo)**.

1. Assegnare un nome allo stack (ad esempio, **VPC-peering-connection**).

1. Accettare i valori predefiniti, quindi selezionare **Next (Avanti)**.

1. Scegli **Riconosco che CloudFormation potrebbe creare risorse IAM**, quindi scegli **Create stack**.

## Creazione di un modello con una policy altamente restrittiva
<a name="create-template-with-highly-restrictive-policy"></a>

Potresti creare una policy altamente restrittiva per collegare in peering il VPC con un altro Account AWS. 

L’esempio seguente mostra come modificare il modello del proprietario della connessione in peering del VPC (l’*account dell’accettante* creato nella fase 1 precedente) in modo che sia di livello più restrittivo.

**Example JSON**  

```
{
  "AWSTemplateFormatVersion":"2010-09-09",
  "Description":"Create a VPC and an assumable role for cross account VPC peering.",
  "Parameters":{
    "PeerRequesterAccountId":{
      "Type":"String"
    }
  },
  "Resources":{
    "peerRole":{
      "Type":"AWS::IAM::Role",
      "Properties":{
        "AssumeRolePolicyDocument":{
          "Statement":[
            {
              "Action":[
                "sts:AssumeRole"
              ],
              "Effect":"Allow",
              "Principal":{
                "AWS":{
                  "Ref":"PeerRequesterAccountId"
                }
              }
            }
          ]
        },
        "Path":"/",
        "Policies":[
          {
            "PolicyDocument":{
              "Statement":[
                {
                  "Action":"ec2:acceptVpcPeeringConnection",
                  "Effect":"Allow",
                  "Resource":{
                    "Fn::Sub":"arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:vpc/${vpc}"
                  }
                },
                {
                  "Action":"ec2:acceptVpcPeeringConnection",
                  "Condition":{
                    "StringEquals":{
                      "ec2:AccepterVpc":{
                        "Fn::Sub":"arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:vpc/${vpc}"
                      }
                    }
                  },
                  "Effect":"Allow",
                  "Resource":{
                    "Fn::Sub":"arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:vpc-peering-connection/*"
                  }
                }
              ],
              "Version":"2012-10-17" 		 	 	 
            },
            "PolicyName":"root"
          }
        ]
      }
    },
    "vpc":{
      "Type":"AWS::EC2::VPC",
      "Properties":{
        "CidrBlock":"10.1.0.0/16",
        "EnableDnsHostnames":false,
        "EnableDnsSupport":false,
        "InstanceTenancy":"default"
      }
    }
  },
  "Outputs":{
    "RoleARN":{
      "Value":{
        "Fn::GetAtt":[
          "peerRole",
          "Arn"
        ]
      }
    },
    "VPCId":{
      "Value":{
        "Ref":"vpc"
      }
    }
  }
}
```

**Example YAML**  

```
AWSTemplateFormatVersion: 2010-09-09
Description: Create a VPC and an assumable role for cross account VPC peering.
Parameters:
  PeerRequesterAccountId:
    Type: String
Resources:
  peerRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Statement:
          - Action:
              - 'sts:AssumeRole'
            Effect: Allow
            Principal:
              AWS:
                Ref: PeerRequesterAccountId
      Path: /
      Policies:
        - PolicyDocument:
            Statement:
              - Action: 'ec2:acceptVpcPeeringConnection'
                Effect: Allow
                Resource:
                  'Fn::Sub': 'arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:vpc/${vpc}'
              - Action: 'ec2:acceptVpcPeeringConnection'
                Condition:
                  StringEquals:
                    'ec2:AccepterVpc':
                      'Fn::Sub': 'arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:vpc/${vpc}'
                Effect: Allow
                Resource:
                  'Fn::Sub': >-
                    arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:vpc-peering-connection/*
            Version: 2012-10-17 		 	 	 
          PolicyName: root
  vpc:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.1.0.0/16
      EnableDnsHostnames: false
      EnableDnsSupport: false
      InstanceTenancy: default
Outputs:
  RoleARN:
    Value:
      'Fn::GetAtt':
        - peerRole
        - Arn
  VPCId:
    Value:
      Ref: vpc
```

Per accedere al VPC, è possibile utilizzare lo stesso modello del richiedente della fase 2 precedente.

Per ulteriori informazioni, consulta [Identity and access management for VPC peering](https://docs.aws.amazon.com/vpc/latest/peering/security-iam.html) nella *Guida al peering di Amazon VPC*.