-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
r/ecs_service: Add NetworkConfiguration #2299
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,7 @@ func resourceAwsEcsService() *schema.Resource { | |
Type: schema.TypeString, | ||
ForceNew: true, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
|
||
"deployment_maximum_percent": { | ||
|
@@ -101,7 +102,25 @@ func resourceAwsEcsService() *schema.Resource { | |
}, | ||
Set: resourceAwsEcsLoadBalancerHash, | ||
}, | ||
|
||
"network_configuration": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
MaxItems: 1, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"security_groups": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
"subnets": { | ||
Type: schema.TypeList, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As the ordering of subnets is not significant shouldn't this field be |
||
Required: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"placement_strategy": { | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
|
@@ -194,6 +213,8 @@ func resourceAwsEcsServiceCreate(d *schema.ResourceData, meta interface{}) error | |
input.Role = aws.String(v.(string)) | ||
} | ||
|
||
input.NetworkConfiguration = expandEcsNetworkConfigration(d.Get("network_configuration").([]interface{})) | ||
|
||
strategies := d.Get("placement_strategy").(*schema.Set).List() | ||
if len(strategies) > 0 { | ||
var ps []*ecs.PlacementStrategy | ||
|
@@ -353,9 +374,36 @@ func resourceAwsEcsServiceRead(d *schema.ResourceData, meta interface{}) error { | |
log.Printf("[ERR] Error setting placement_constraints for (%s): %s", d.Id(), err) | ||
} | ||
|
||
if err := d.Set("network_configuration", flattenEcsNetworkConfigration(service.NetworkConfiguration)); err != nil { | ||
log.Printf("[ERR] Error setting network_configuration for (%s): %s", d.Id(), err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason why we should silently fail here instead of returning error? |
||
} | ||
|
||
return nil | ||
} | ||
|
||
func flattenEcsNetworkConfigration(nc *ecs.NetworkConfiguration) []interface{} { | ||
if nc == nil { | ||
return nil | ||
} | ||
result := make(map[string]interface{}) | ||
result["security_groups"] = flattenStringList(nc.AwsvpcConfiguration.SecurityGroups) | ||
result["subnets"] = flattenStringList(nc.AwsvpcConfiguration.Subnets) | ||
return []interface{}{result} | ||
} | ||
|
||
func expandEcsNetworkConfigration(nc []interface{}) *ecs.NetworkConfiguration { | ||
if len(nc) == 0 { | ||
return nil | ||
} | ||
awsVpcConfig := &ecs.AwsVpcConfiguration{} | ||
raw := nc[0].(map[string]interface{}) | ||
if val, ok := raw["security_groups"]; ok { | ||
awsVpcConfig.SecurityGroups = expandStringList(val.([]interface{})) | ||
} | ||
awsVpcConfig.Subnets = expandStringList(raw["subnets"].([]interface{})) | ||
return &ecs.NetworkConfiguration{AwsvpcConfiguration: awsVpcConfig} | ||
} | ||
|
||
func flattenServicePlacementConstraints(pcs []*ecs.PlacementConstraint) []map[string]interface{} { | ||
if len(pcs) == 0 { | ||
return nil | ||
|
@@ -418,6 +466,10 @@ func resourceAwsEcsServiceUpdate(d *schema.ResourceData, meta interface{}) error | |
} | ||
} | ||
|
||
if d.HasChange("network_configration") { | ||
input.NetworkConfiguration = expandEcsNetworkConfigration(d.Get("network_configuration").([]interface{})) | ||
} | ||
|
||
// Retry due to IAM & ECS eventual consistency | ||
err := resource.Retry(2*time.Minute, func() *resource.RetryError { | ||
out, err := conn.UpdateService(&input) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,6 +118,7 @@ func validateAwsEcsTaskDefinitionNetworkMode(v interface{}, k string) (ws []stri | |
validTypes := map[string]struct{}{ | ||
"bridge": {}, | ||
"host": {}, | ||
"awsvpc": {}, | ||
"none": {}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not necessarily a blocker for this PR, but I'm pondering with an idea of removing this validation completely as theoretically the list may grow again (knowing how complex and big the container networking "market" is). What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my opinion, it's better to prevent error as early as possible. When constructing state is better than when requesting API. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My opinion comes from users' perspective but you have to consider maintainability more than me... |
||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,7 @@ into consideration during task placement. The maximum number of | |
* `load_balancer` - (Optional) A load balancer block. Load balancers documented below. | ||
* `placement_constraints` - (Optional) rules that are taken into consideration during task placement. Maximum number of | ||
`placement_constraints` is `10`. Defined below. | ||
* `network_configuration` - (Optional) The network configuration for the service. This parameter is required for task definitions that use the awsvpc network mode to receive their own Elastic Network Interface, and it is not supported for other network modes. | ||
|
||
-> **Note:** As a result of an AWS limitation, a single `load_balancer` can be attached to the ECS service at most. See [related docs](http://docs.aws.amazon.com/AmazonECS/latest/developerguide/service-load-balancing.html#load-balancing-concepts). | ||
|
||
|
@@ -93,7 +94,12 @@ For more information, see [Cluster Query Language in the Amazon EC2 Container | |
Service Developer | ||
Guide](http://docs.aws.amazon.com/AmazonECS/latest/developerguide/cluster-query-language.html). | ||
|
||
## network_configuration | ||
|
||
`network_configuration` support the following: | ||
* `subnets` - (Required) The subnets associated with the task or service. | ||
* `security_groups` - (Optional) The security groups associated with the task or service. If you do not specify a security group, the default security group for the VPC is used. | ||
For more information, see [Task Networking](http://docs.aws.amazon.com/AmazonECS/latest/developerguidetask-networking.html) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The link is wrong, correct url is: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-networking.html There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The documentation has already been fixed in #2694 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, but it is still wrong, in fact, the link is https. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ozbillwang Sure. Thanks! |
||
|
||
## Attributes Reference | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As the ordering of SGs is not significant shouldn't this field be
TypeSet
to avoid potential spurious diffs in case the list of SGs comes back in a different order from the API?