View a markdown version of this page

Usare CreateVpcEndpoint con un AWS SDK o CLI - Amazon Elastic Compute Cloud

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

Usare CreateVpcEndpoint con un AWS SDK o CLI

I seguenti esempi di codice mostrano come usare CreateVpcEndpoint.

Gli esempi di operazioni sono estratti di codice da programmi più grandi e devono essere eseguiti nel contesto. È possibile visualizzare questa operazione nel contesto nel seguente esempio di codice:

CLI
AWS CLI

Esempio 1: come creare un endpoint del gateway.

L’esempio create-vpc-endpoint seguente crea un endpoint VPC del gateway tra il VPC vpc-1a2b3c4d e Amazon S3 nella Regione us-west-2 e associa la tabella di routing rtb-11aa22bb all’endpoint.

aws ec2 create-vpc-endpoint \ --vpc-id vpc-1a2b3c4d \ --service-name com.amazonaws.us-west-2.s3 \ --route-table-ids rtb-11aa22bb

Output:

{ "VpcEndpoint": { "PolicyDocument": "{\"Version\":\"2008-10-17\",\"Statement\":[{\"Sid\":\"\",\"Effect\":\"Allow\",\"Principal\":\"*\",\"Action\":\"*\",\"Resource\":\"*\"}]}", "VpcId": "vpc-1a2b3c4d", "State": "available", "ServiceName": "com.amazonaws.us-west-2.s3", "RouteTableIds": [ "rtb-11aa22bb" ], "VpcEndpointId": "vpc-1a2b3c4d", "CreationTimestamp": "2015-05-15T09:40:50Z" } }

Per ulteriori informazioni, vedere Crea un endpoint gateway nel AWS PrivateLink Guida per l'utente.

Esempio 2: come creare un endpoint dell’interfaccia

L’esempio create-vpc-endpoint seguente crea un endpoint VPC dell’interfaccia tra il VPC vpc-1a2b3c4d e Amazon S3 nella Regione us-west-2. Il comando crea l’endpoint nella sottorete subnet-1a2b3c4d, lo associa al gruppo di sicurezza sg-1a2b3c4d e aggiunge un tag con la chiave “Service” e il valore di “S3”.

aws ec2 create-vpc-endpoint \ --vpc-id vpc-1a2b3c4d \ --vpc-endpoint-type Interface \ --service-name com.amazonaws.us-west-2.s3 \ --subnet-ids subnet-7b16de0c \ --security-group-ids sg-1a2b3c4d \ --tag-specifications ResourceType=vpc-endpoint,Tags=[{Key=service,Value=S3}]

Output:

{ "VpcEndpoint": { "VpcEndpointId": "vpce-1a2b3c4d5e6f1a2b3", "VpcEndpointType": "Interface", "VpcId": "vpc-1a2b3c4d", "ServiceName": "com.amazonaws.us-west-2.s3", "State": "pending", "RouteTableIds": [], "SubnetIds": [ "subnet-1a2b3c4d" ], "Groups": [ { "GroupId": "sg-1a2b3c4d", "GroupName": "default" } ], "PrivateDnsEnabled": false, "RequesterManaged": false, "NetworkInterfaceIds": [ "eni-0b16f0581c8ac6877" ], "DnsEntries": [ { "DnsName": "*.vpce-1a2b3c4d5e6f1a2b3-9hnenorg.s3.us-west-2.vpce.amazonaws.com", "HostedZoneId": "Z7HUB22UULQXV" }, { "DnsName": "*.vpce-1a2b3c4d5e6f1a2b3-9hnenorg-us-west-2c.s3.us-west-2.vpce.amazonaws.com", "HostedZoneId": "Z7HUB22UULQXV" } ], "CreationTimestamp": "2021-03-05T14:46:16.030000+00:00", "Tags": [ { "Key": "service", "Value": "S3" } ], "OwnerId": "123456789012" } }

Per ulteriori informazioni, vedere Crea un endpoint VPC di interfaccia nel AWS PrivateLink Guida per l'utente.

Esempio 3: come creare un endpoint Gateway Load Balancer

Quanto segue create-vpc-endpoint l'esempio crea un endpoint Gateway Load Balancer tra VPC vpc-1a2b3c4d5e6f7a8b9 e un servizio configurato utilizzando un Gateway Load Balancer.

aws ec2 create-vpc-endpoint \ --service-name com.amazonaws.vpce.us-west-2.vpce-svc-123123a1c43abc123 \ --vpc-endpoint-type GatewayLoadBalancer \ --vpc-id vpc-1a2b3c4d5e6f7a8b9 \ --subnet-ids subnet-0011aabbcc2233445

Output:

{ "VpcEndpoint": { "VpcEndpointId": "vpce-aabbaabbaabbaabba", "VpcEndpointType": "GatewayLoadBalancer", "VpcId": "vpc-1a2b3c4d5e6f7a8b9", "ServiceName": "com.amazonaws.vpce.us-west-2.vpce-svc-123123a1c43abc123", "State": "pending", "SubnetIds": [ "subnet-0011aabbcc2233445" ], "RequesterManaged": false, "NetworkInterfaceIds": [ "eni-01010120203030405" ], "CreationTimestamp": "2020-11-11T08:06:03.522Z", "OwnerId": "123456789012" } }

Per ulteriori informazioni, vedere Endpoint Gateway Load Balancer nel AWS PrivateLink Guida per l'utente.

Esempio 4: come creare un endpoint della risorsa

L’esempio create-vpc-endpoint seguente crea un endpoint delle risorse.

aws ec2 create-vpc-endpoint \ --vpc-endpoint-type Resource \ --vpc-id vpc-1a2b3c4d5e6f7a8b9 \ --subnet-ids subnet-0011aabbcc2233445 \ --resource-configuration-arn arn:aws:vpc-lattice-us-east-1:123456789012:resourceconfiguration/rcfg-0123abcde98765432

Output:

{ "VpcEndpoint": { "VpcEndpointId": "vpce-00939a7ed9EXAMPLE", "VpcEndpointType": "Resource", "VpcId": "vpc-1a2b3c4d5e6f7a8b9", "State": "Pending", "SubnetIds": [ "subnet-0011aabbcc2233445" ], "Groups": [ { "GroupId": "sg-03e2f15fbfc09b000", "GroupName": "default" } ], "IpAddressType": "IPV4", "PrivateDnsEnabled": false, "CreationTimestamp": "2025-02-06T23:38:49.525000+00:00", "Tags": [], "OwnerId": "123456789012", "ResourceConfigurationArn": "arn:aws:vpc-lattice:us-west-2:123456789012:resourceconfiguration/rcfg-0123abcde98765432" } }

Per ulteriori informazioni, vedere Endpoint delle risorse nel AWS PrivateLink Guida per l'utente.

Esempio 5: come creare un endpoint della rete di servizi

L’esempio create-vpc-endpoint seguente crea un endpoint della rete di servizi.

aws ec2 create-vpc-endpoint \ --vpc-endpoint-type ServiceNetwork \ --vpc-id vpc-1a2b3c4d5e6f7a8b9 \ --subnet-ids subnet-0011aabbcc2233445 \ --service-network-arn arn:aws:vpc-lattice:us-west-2:123456789012:servicenetwork/sn-0101abcd5432abcd0 \ --security-group-ids sg-0123456789012abcd

Output:

{ "VpcEndpoint": { "VpcEndpointId": "vpce-0f00567fa8EXAMPLE", "VpcEndpointType": "ServiceNetwork", "VpcId": "vpc-1a2b3c4d5e6f7a8b9", "State": "Pending", "SubnetIds": [ "subnet-0011aabbcc2233445" ], "Groups": [ { "GroupId": "sg-0123456789012abcd", "GroupName": "my-security-group" } ], "IpAddressType": "IPV4", "PrivateDnsEnabled": false, "CreationTimestamp": "2025-02-06T23:44:20.449000+00:00", "Tags": [], "OwnerId": "123456789012", "ServiceNetworkArn": "arn:aws:vpc-lattice:us-west-2:123456789012:servicenetwork/sn-0101abcd5432abcd0" } }

Per ulteriori informazioni, vedere Endpoint della rete di assistenza nel AWS PrivateLink Guida per l'utente.

  • Per i dettagli sull'API, vedi CreateVpcEndpoint nel AWS CLI Riferimento al comando.

PHP
SDK per PHP
Nota

C'è di più su GitHub. Trova l’esempio completo e scopri di più sulla configurazione e l’esecuzione nel repository di esempi di codice AWS.

/** * @param string $serviceName * @param string $vpcId * @param array $routeTableIds * @return array */ public function createVpcEndpoint(string $serviceName, string $vpcId, array $routeTableIds): array { try { $result = $this->ec2Client->createVpcEndpoint([ 'ServiceName' => $serviceName, 'VpcId' => $vpcId, 'RouteTableIds' => $routeTableIds, ]); return $result["VpcEndpoint"]; } catch(Ec2Exception $caught){ echo "There was a problem creating the VPC Endpoint: {$caught->getAwsErrorMessage()}\n"; throw $caught; } }
  • Per i dettagli sull'API, vedi CreateVpcEndpoint nel AWS SDK per PHP Riferimento API.

PowerShell
Utensili per PowerShell V4

Esempio 1: questo esempio crea un nuovo endpoint VPC per il servizio com.amazonaws.eu-west-1.s3 nel VPC vpc-0fc1ff23f45b678eb

New-EC2VpcEndpoint -ServiceName com.amazonaws.eu-west-1.s3 -VpcId vpc-0fc1ff23f45b678eb

Output:

ClientToken VpcEndpoint ----------- ----------- Amazon.EC2.Model.VpcEndpoint
  • Per i dettagli sull'API, vedi CreateVpcEndpoint nel AWS Strumenti per PowerShell Riferimento al cmdlet (V4).

Utensili per PowerShell V5

Esempio 1: questo esempio crea un nuovo endpoint VPC per il servizio com.amazonaws.eu-west-1.s3 nel VPC vpc-0fc1ff23f45b678eb

New-EC2VpcEndpoint -ServiceName com.amazonaws.eu-west-1.s3 -VpcId vpc-0fc1ff23f45b678eb

Output:

ClientToken VpcEndpoint ----------- ----------- Amazon.EC2.Model.VpcEndpoint
  • Per i dettagli sull'API, vedi CreateVpcEndpoint nel AWS Strumenti per PowerShell Riferimento al cmdlet (V5).

Python
SDK for Python (Boto3)
Nota

C'è di più su GitHub. Trova l’esempio completo e scopri di più sulla configurazione e l’esecuzione nel repository di esempi di codice AWS.

class VpcWrapper: """Encapsulates Amazon Elastic Compute Cloud (Amazon EC2) Amazon Virtual Private Cloud actions.""" def __init__(self, ec2_client: boto3.client): """ Initializes the VpcWrapper with an EC2 client. :param ec2_client: A Boto3 Amazon EC2 client. This client provides low-level access to AWS EC2 services. """ self.ec2_client = ec2_client @classmethod def from_client(cls) -> "VpcWrapper": """ Creates a VpcWrapper instance with a default EC2 client. :return: An instance of VpcWrapper initialized with the default EC2 client. """ ec2_client = boto3.client("ec2") return cls(ec2_client) def create_vpc_endpoint( self, vpc_id: str, service_name: str, route_table_ids: list[str] ) -> Dict[str, Any]: """ Creates a new VPC endpoint for the specified service and associates it with the specified route tables. :param vpc_id: The ID of the VPC to create the endpoint in. :param service_name: The name of the service to create the endpoint for. :param route_table_ids: A list of IDs of the route tables to associate with the endpoint. :return: A dictionary representing the newly created VPC endpoint. """ try: response = self.ec2_client.create_vpc_endpoint( VpcId=vpc_id, ServiceName=service_name, RouteTableIds=route_table_ids, ) return response["VpcEndpoint"] except ClientError as err: logger.error( "Couldn't create VPC endpoint for service %s. Here's why: %s: %s", service_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise
  • Per i dettagli sull'API, vedi CreateVpcEndpoint nel AWS Riferimento alle API SDK per Python (Boto3).

SAP ABAP
SDK per SAP ABAP
Nota

C'è di più su GitHub. Trova l’esempio completo e scopri di più sulla configurazione e l’esecuzione nel repository di esempi di codice AWS.

" iv_vpc_id = 'vpc-abc123' " iv_service_name = 'com.amazonaws.region.service' TRY. oo_result = lo_ec2->createvpcendpoint( " oo_result is returned for testing purposes. " iv_vpcid = iv_vpc_id iv_servicename = iv_service_name it_routetableids = it_route_table_ids ). DATA(lv_vpc_endpoint_id) = oo_result->get_vpcendpoint( )->get_vpcendpointid( ). MESSAGE 'Created VPC endpoint.' TYPE 'I'. CATCH /aws1/cx_rt_service_generic INTO DATA(lo_exception). DATA(lv_error) = |"{ lo_exception->av_err_code }" - { lo_exception->av_err_msg }|. MESSAGE lv_error TYPE 'E'. ENDTRY.
  • Per i dettagli sull'API, vedi CreateVpcEndpoint nel AWS Riferimento all'API SDK per SAP ABAP.

Per un elenco completo di AWS Guide per sviluppatori SDK ed esempi di codice, consulta Crea EC2 risorse Amazon utilizzando un AWS SDK. Questo argomento include anche informazioni su come iniziare e dettagli sulle versioni precedenti dell'SDK.