com.pulumi.aws.ec2.SecurityGroup Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aws Show documentation
Show all versions of aws Show documentation
A Pulumi package for creating and managing Amazon Web Services (AWS) cloud resources.
// *** WARNING: this file was generated by pulumi-java-gen. ***
// *** Do not edit by hand unless you're certain you know what you are doing! ***
package com.pulumi.aws.ec2;
import com.pulumi.aws.Utilities;
import com.pulumi.aws.ec2.SecurityGroupArgs;
import com.pulumi.aws.ec2.inputs.SecurityGroupState;
import com.pulumi.aws.ec2.outputs.SecurityGroupEgress;
import com.pulumi.aws.ec2.outputs.SecurityGroupIngress;
import com.pulumi.core.Output;
import com.pulumi.core.annotations.Export;
import com.pulumi.core.annotations.ResourceType;
import com.pulumi.core.internal.Codegen;
import java.lang.Boolean;
import java.lang.String;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Nullable;
/**
* Provides a security group resource.
*
* > **NOTE:** Avoid using the `ingress` and `egress` arguments of the `aws.ec2.SecurityGroup` resource to configure in-line rules, as they struggle with managing multiple CIDR blocks, and, due to the historical lack of unique IDs, tags and descriptions. To avoid these problems, use the current best practice of the `aws.vpc.SecurityGroupEgressRule` and `aws.vpc.SecurityGroupIngressRule` resources with one CIDR block per rule.
*
* !> **WARNING:** You should not use the `aws.ec2.SecurityGroup` resource with _in-line rules_ (using the `ingress` and `egress` arguments of `aws.ec2.SecurityGroup`) in conjunction with the `aws.vpc.SecurityGroupEgressRule` and `aws.vpc.SecurityGroupIngressRule` resources or the `aws.ec2.SecurityGroupRule` resource. Doing so may cause rule conflicts, perpetual differences, and result in rules being overwritten.
*
* > **NOTE:** Referencing Security Groups across VPC peering has certain restrictions. More information is available in the [VPC Peering User Guide](https://docs.aws.amazon.com/vpc/latest/peering/vpc-peering-security-groups.html).
*
* > **NOTE:** Due to [AWS Lambda improved VPC networking changes that began deploying in September 2019](https://aws.amazon.com/blogs/compute/announcing-improved-vpc-networking-for-aws-lambda-functions/), security groups associated with Lambda Functions can take up to 45 minutes to successfully delete. To allow for successful deletion, the provider will wait for at least 45 minutes even if a shorter delete timeout is specified.
*
* > **NOTE:** The `cidr_blocks` and `ipv6_cidr_blocks` parameters are optional in the `ingress` and `egress` blocks. If nothing is specified, traffic will be blocked as described in _NOTE on Egress rules_ later.
*
* ## Example Usage
*
* ### Basic Usage
*
* <!--Start PulumiCodeChooser -->
*
* {@code
* package generated_program;
*
* import com.pulumi.Context;
* import com.pulumi.Pulumi;
* import com.pulumi.core.Output;
* import com.pulumi.aws.ec2.SecurityGroup;
* import com.pulumi.aws.ec2.SecurityGroupArgs;
* import com.pulumi.aws.vpc.SecurityGroupIngressRule;
* import com.pulumi.aws.vpc.SecurityGroupIngressRuleArgs;
* import com.pulumi.aws.vpc.SecurityGroupEgressRule;
* import com.pulumi.aws.vpc.SecurityGroupEgressRuleArgs;
* import java.util.List;
* import java.util.ArrayList;
* import java.util.Map;
* import java.io.File;
* import java.nio.file.Files;
* import java.nio.file.Paths;
*
* public class App {
* public static void main(String[] args) {
* Pulumi.run(App::stack);
* }
*
* public static void stack(Context ctx) {
* var allowTls = new SecurityGroup("allowTls", SecurityGroupArgs.builder()
* .name("allow_tls")
* .description("Allow TLS inbound traffic and all outbound traffic")
* .vpcId(main.id())
* .tags(Map.of("Name", "allow_tls"))
* .build());
*
* var allowTlsIpv4 = new SecurityGroupIngressRule("allowTlsIpv4", SecurityGroupIngressRuleArgs.builder()
* .securityGroupId(allowTls.id())
* .cidrIpv4(main.cidrBlock())
* .fromPort(443)
* .ipProtocol("tcp")
* .toPort(443)
* .build());
*
* var allowTlsIpv6 = new SecurityGroupIngressRule("allowTlsIpv6", SecurityGroupIngressRuleArgs.builder()
* .securityGroupId(allowTls.id())
* .cidrIpv6(main.ipv6CidrBlock())
* .fromPort(443)
* .ipProtocol("tcp")
* .toPort(443)
* .build());
*
* var allowAllTrafficIpv4 = new SecurityGroupEgressRule("allowAllTrafficIpv4", SecurityGroupEgressRuleArgs.builder()
* .securityGroupId(allowTls.id())
* .cidrIpv4("0.0.0.0/0")
* .ipProtocol("-1")
* .build());
*
* var allowAllTrafficIpv6 = new SecurityGroupEgressRule("allowAllTrafficIpv6", SecurityGroupEgressRuleArgs.builder()
* .securityGroupId(allowTls.id())
* .cidrIpv6("::/0")
* .ipProtocol("-1")
* .build());
*
* }
* }
* }
*
* <!--End PulumiCodeChooser -->
*
* > **NOTE on Egress rules:** By default, AWS creates an `ALLOW ALL` egress rule when creating a new Security Group inside of a VPC. When creating a new Security Group inside a VPC, **this provider will remove this default rule**, and require you specifically re-create it if you desire that rule. We feel this leads to fewer surprises in terms of controlling your egress rules. If you desire this rule to be in place, you can use this `egress` block:
*
* <!--Start PulumiCodeChooser -->
*
* {@code
* package generated_program;
*
* import com.pulumi.Context;
* import com.pulumi.Pulumi;
* import com.pulumi.core.Output;
* import com.pulumi.aws.ec2.SecurityGroup;
* import com.pulumi.aws.ec2.SecurityGroupArgs;
* import com.pulumi.aws.ec2.inputs.SecurityGroupEgressArgs;
* import java.util.List;
* import java.util.ArrayList;
* import java.util.Map;
* import java.io.File;
* import java.nio.file.Files;
* import java.nio.file.Paths;
*
* public class App {
* public static void main(String[] args) {
* Pulumi.run(App::stack);
* }
*
* public static void stack(Context ctx) {
* var example = new SecurityGroup("example", SecurityGroupArgs.builder()
* .egress(SecurityGroupEgressArgs.builder()
* .fromPort(0)
* .toPort(0)
* .protocol("-1")
* .cidrBlocks("0.0.0.0/0")
* .ipv6CidrBlocks("::/0")
* .build())
* .build());
*
* }
* }
* }
*
* <!--End PulumiCodeChooser -->
*
* ### Usage With Prefix List IDs
*
* Prefix Lists are either managed by AWS internally, or created by the customer using a
* Prefix List resource. Prefix Lists provided by
* AWS are associated with a prefix list name, or service name, that is linked to a specific region.
* Prefix list IDs are exported on VPC Endpoints, so you can use this format:
*
* <!--Start PulumiCodeChooser -->
*
* {@code
* package generated_program;
*
* import com.pulumi.Context;
* import com.pulumi.Pulumi;
* import com.pulumi.core.Output;
* import com.pulumi.aws.ec2.VpcEndpoint;
* import com.pulumi.aws.ec2.SecurityGroup;
* import com.pulumi.aws.ec2.SecurityGroupArgs;
* import com.pulumi.aws.ec2.inputs.SecurityGroupEgressArgs;
* import java.util.List;
* import java.util.ArrayList;
* import java.util.Map;
* import java.io.File;
* import java.nio.file.Files;
* import java.nio.file.Paths;
*
* public class App {
* public static void main(String[] args) {
* Pulumi.run(App::stack);
* }
*
* public static void stack(Context ctx) {
* var myEndpoint = new VpcEndpoint("myEndpoint");
*
* var example = new SecurityGroup("example", SecurityGroupArgs.builder()
* .egress(SecurityGroupEgressArgs.builder()
* .fromPort(0)
* .toPort(0)
* .protocol("-1")
* .prefixListIds(myEndpoint.prefixListId())
* .build())
* .build());
*
* }
* }
* }
*
* <!--End PulumiCodeChooser -->
*
* You can also find a specific Prefix List using the `aws.ec2.getPrefixList` data source.
*
* ### Removing All Ingress and Egress Rules
*
* The `ingress` and `egress` arguments are processed in attributes-as-blocks mode. Due to this, removing these arguments from the configuration will **not** cause the provider to destroy the managed rules. To subsequently remove all managed ingress and egress rules:
*
* <!--Start PulumiCodeChooser -->
*
* {@code
* package generated_program;
*
* import com.pulumi.Context;
* import com.pulumi.Pulumi;
* import com.pulumi.core.Output;
* import com.pulumi.aws.ec2.SecurityGroup;
* import com.pulumi.aws.ec2.SecurityGroupArgs;
* import java.util.List;
* import java.util.ArrayList;
* import java.util.Map;
* import java.io.File;
* import java.nio.file.Files;
* import java.nio.file.Paths;
*
* public class App {
* public static void main(String[] args) {
* Pulumi.run(App::stack);
* }
*
* public static void stack(Context ctx) {
* var example = new SecurityGroup("example", SecurityGroupArgs.builder()
* .name("sg")
* .vpcId(exampleAwsVpc.id())
* .ingress()
* .egress()
* .build());
*
* }
* }
* }
*
* <!--End PulumiCodeChooser -->
*
* ### Recreating a Security Group
*
* A simple security group `name` change "forces new" the security group--the provider destroys the security group and creates a new one. (Likewise, `description`, `name_prefix`, or `vpc_id` [cannot be changed](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/working-with-security-groups.html#creating-security-group).) Attempting to recreate the security group leads to a variety of complications depending on how it is used.
*
* Security groups are generally associated with other resources--**more than 100** AWS Provider resources reference security groups. Referencing a resource from another resource creates a one-way dependency. For example, if you create an EC2 `aws.ec2.Instance` that has a `vpc_security_group_ids` argument that refers to an `aws.ec2.SecurityGroup` resource, the `aws.ec2.SecurityGroup` is a dependent of the `aws.ec2.Instance`. Because of this, the provider will create the security group first so that it can then be associated with the EC2 instance.
*
* However, the dependency relationship actually goes both directions causing the _Security Group Deletion Problem_. AWS does not allow you to delete the security group associated with another resource (_e.g._, the `aws.ec2.Instance`).
*
* The provider does not model bi-directional dependencies like this, but, even if it did, simply knowing the dependency situation would not be enough to solve it. For example, some resources must always have an associated security group while others don't need to. In addition, when the `aws.ec2.SecurityGroup` resource attempts to recreate, it receives a dependent object error, which does not provide information on whether the dependent object is a security group rule or, for example, an associated EC2 instance. Within the provider, the associated resource (_e.g._, `aws.ec2.Instance`) does not receive an error when the `aws.ec2.SecurityGroup` is trying to recreate even though that is where changes to the associated resource would need to take place (_e.g._, removing the security group association).
*
* Despite these sticky problems, below are some ways to improve your experience when you find it necessary to recreate a security group.
*
* ### Shorter timeout
*
* (This example is one approach to recreating security groups. For more information on the challenges and the _Security Group Deletion Problem_, see the section above.)
*
* If destroying a security group takes a long time, it may be because the provider cannot distinguish between a dependent object (_e.g._, a security group rule or EC2 instance) that is _in the process of being deleted_ and one that is not. In other words, it may be waiting for a train that isn't scheduled to arrive. To fail faster, shorten the `delete` timeout from the default timeout:
*
* <!--Start PulumiCodeChooser -->
*
* {@code
* package generated_program;
*
* import com.pulumi.Context;
* import com.pulumi.Pulumi;
* import com.pulumi.core.Output;
* import com.pulumi.aws.ec2.SecurityGroup;
* import com.pulumi.aws.ec2.SecurityGroupArgs;
* import java.util.List;
* import java.util.ArrayList;
* import java.util.Map;
* import java.io.File;
* import java.nio.file.Files;
* import java.nio.file.Paths;
*
* public class App {
* public static void main(String[] args) {
* Pulumi.run(App::stack);
* }
*
* public static void stack(Context ctx) {
* var example = new SecurityGroup("example", SecurityGroupArgs.builder()
* .name("izizavle")
* .build());
*
* }
* }
* }
*
* <!--End PulumiCodeChooser -->
*
* ### Provisioners
*
* (This example is one approach to recreating security groups. For more information on the challenges and the _Security Group Deletion Problem_, see the section above.)
*
* **DISCLAIMER:** We **_HIGHLY_** recommend using one of the above approaches and _NOT_ using local provisioners. Provisioners, like the one shown below, should be considered a **last resort** since they are _not readable_, _require skills outside standard configuration_, are _error prone_ and _difficult to maintain_, are not compatible with cloud environments and upgrade tools, require AWS CLI installation, and are subject to changes outside the AWS Provider.
*
* <!--Start PulumiCodeChooser -->
*
* {@code
* package generated_program;
*
* import com.pulumi.Context;
* import com.pulumi.Pulumi;
* import com.pulumi.core.Output;
* import com.pulumi.aws.ec2.Ec2Functions;
* import com.pulumi.aws.ec2.inputs.GetSecurityGroupArgs;
* import com.pulumi.aws.ec2.SecurityGroup;
* import com.pulumi.aws.ec2.SecurityGroupArgs;
* import com.pulumi.command.local.Command;
* import com.pulumi.command.local.CommandArgs;
* import com.pulumi.null.Resource;
* import com.pulumi.null.ResourceArgs;
* import com.pulumi.resources.CustomResourceOptions;
* import java.util.List;
* import java.util.ArrayList;
* import java.util.Map;
* import java.io.File;
* import java.nio.file.Files;
* import java.nio.file.Paths;
*
* public class App {
* public static void main(String[] args) {
* Pulumi.run(App::stack);
* }
*
* public static void stack(Context ctx) {
* final var default = Ec2Functions.getSecurityGroup(GetSecurityGroupArgs.builder()
* .name("default")
* .build());
*
* var example = new SecurityGroup("example", SecurityGroupArgs.builder()
* .name("sg")
* .tags(Map.ofEntries(
* Map.entry("workaround1", "tagged-name"),
* Map.entry("workaround2", default_.id())
* ))
* .build());
*
* var exampleProvisioner0 = new Command("exampleProvisioner0", CommandArgs.builder()
* .create("true")
* .update("true")
* .delete("""
* ENDPOINT_ID=`aws ec2 describe-vpc-endpoints --filters "Name=tag:Name,Values=%s" --query "VpcEndpoints[0].VpcEndpointId" --output text` &&
* aws ec2 modify-vpc-endpoint --vpc-endpoint-id ${ENDPOINT_ID} --add-security-group-ids %s --remove-security-group-ids %s
* ", tags.workaround1(),tags.workaround2(),id))
* .build(), CustomResourceOptions.builder()
* .dependsOn(example)
* .build());
*
* var exampleResource = new Resource("exampleResource", ResourceArgs.builder()
* .triggers(Map.of("rerun_upon_change_of", StdFunctions.join(JoinArgs.builder()
* .separator(",")
* .input(exampleAwsVpcEndpoint.securityGroupIds())
* .build()).result()))
* .build());
*
* var exampleResourceProvisioner0 = new Command("exampleResourceProvisioner0", CommandArgs.builder()
* .create("""
* aws ec2 modify-vpc-endpoint --vpc-endpoint-id %s --remove-security-group-ids %s
* ", exampleAwsVpcEndpoint.id(),default_.id()))
* .build(), CustomResourceOptions.builder()
* .dependsOn(exampleResource)
* .build());
*
* }
* }
* }
*
* <!--End PulumiCodeChooser -->
*
* ## Import
*
* Using `pulumi import`, import Security Groups using the security group `id`. For example:
*
* ```sh
* $ pulumi import aws:ec2/securityGroup:SecurityGroup elb_sg sg-903004f8
* ```
*
*/
@ResourceType(type="aws:ec2/securityGroup:SecurityGroup")
public class SecurityGroup extends com.pulumi.resources.CustomResource {
/**
* ARN of the security group.
*
*/
@Export(name="arn", refs={String.class}, tree="[0]")
private Output arn;
/**
* @return ARN of the security group.
*
*/
public Output arn() {
return this.arn;
}
/**
* Security group description. Defaults to `Managed by Pulumi`. Cannot be `""`. **NOTE**: This field maps to the AWS `GroupDescription` attribute, for which there is no Update API. If you'd like to classify your security groups in a way that can be updated, use `tags`.
*
*/
@Export(name="description", refs={String.class}, tree="[0]")
private Output description;
/**
* @return Security group description. Defaults to `Managed by Pulumi`. Cannot be `""`. **NOTE**: This field maps to the AWS `GroupDescription` attribute, for which there is no Update API. If you'd like to classify your security groups in a way that can be updated, use `tags`.
*
*/
public Output description() {
return this.description;
}
/**
* Configuration block for egress rules. Can be specified multiple times for each egress rule. Each egress block supports fields documented below. This argument is processed in attribute-as-blocks mode.
*
*/
@Export(name="egress", refs={List.class,SecurityGroupEgress.class}, tree="[0,1]")
private Output> egress;
/**
* @return Configuration block for egress rules. Can be specified multiple times for each egress rule. Each egress block supports fields documented below. This argument is processed in attribute-as-blocks mode.
*
*/
public Output> egress() {
return this.egress;
}
/**
* Configuration block for ingress rules. Can be specified multiple times for each ingress rule. Each ingress block supports fields documented below. This argument is processed in attribute-as-blocks mode.
*
*/
@Export(name="ingress", refs={List.class,SecurityGroupIngress.class}, tree="[0,1]")
private Output> ingress;
/**
* @return Configuration block for ingress rules. Can be specified multiple times for each ingress rule. Each ingress block supports fields documented below. This argument is processed in attribute-as-blocks mode.
*
*/
public Output> ingress() {
return this.ingress;
}
/**
* Name of the security group. If omitted, the provider will assign a random, unique name.
*
*/
@Export(name="name", refs={String.class}, tree="[0]")
private Output name;
/**
* @return Name of the security group. If omitted, the provider will assign a random, unique name.
*
*/
public Output name() {
return this.name;
}
/**
* Creates a unique name beginning with the specified prefix. Conflicts with `name`.
*
*/
@Export(name="namePrefix", refs={String.class}, tree="[0]")
private Output namePrefix;
/**
* @return Creates a unique name beginning with the specified prefix. Conflicts with `name`.
*
*/
public Output namePrefix() {
return this.namePrefix;
}
/**
* Owner ID.
*
*/
@Export(name="ownerId", refs={String.class}, tree="[0]")
private Output ownerId;
/**
* @return Owner ID.
*
*/
public Output ownerId() {
return this.ownerId;
}
/**
* Instruct the provider to revoke all of the Security Groups attached ingress and egress rules before deleting the rule itself. This is normally not needed, however certain AWS services such as Elastic Map Reduce may automatically add required rules to security groups used with the service, and those rules may contain a cyclic dependency that prevent the security groups from being destroyed without removing the dependency first. Default `false`.
*
*/
@Export(name="revokeRulesOnDelete", refs={Boolean.class}, tree="[0]")
private Output* @Nullable */ Boolean> revokeRulesOnDelete;
/**
* @return Instruct the provider to revoke all of the Security Groups attached ingress and egress rules before deleting the rule itself. This is normally not needed, however certain AWS services such as Elastic Map Reduce may automatically add required rules to security groups used with the service, and those rules may contain a cyclic dependency that prevent the security groups from being destroyed without removing the dependency first. Default `false`.
*
*/
public Output> revokeRulesOnDelete() {
return Codegen.optional(this.revokeRulesOnDelete);
}
/**
* Map of tags to assign to the resource. If configured with a provider `default_tags` configuration block present, tags with matching keys will overwrite those defined at the provider-level.
*
*/
@Export(name="tags", refs={Map.class,String.class}, tree="[0,1,1]")
private Output* @Nullable */ Map> tags;
/**
* @return Map of tags to assign to the resource. If configured with a provider `default_tags` configuration block present, tags with matching keys will overwrite those defined at the provider-level.
*
*/
public Output>> tags() {
return Codegen.optional(this.tags);
}
/**
* A map of tags assigned to the resource, including those inherited from the provider `default_tags` configuration block.
*
* @deprecated
* Please use `tags` instead.
*
*/
@Deprecated /* Please use `tags` instead. */
@Export(name="tagsAll", refs={Map.class,String.class}, tree="[0,1,1]")
private Output
© 2015 - 2025 Weber Informatics LLC | Privacy Policy