Get public DNS for aws_autoscaling_group instances - amazon-ec2

I want to output the public DNS of the EC2s that make up my auto scaling group:
resource "aws_launch_configuration" "instances" {
image_id = "ami-0fad7824ed21125b1"
instance_type = "${var.instance_type}"
security_groups = ["${aws_security_group.security_group_ec2.id}"]
key_name = "${var.key_pair_name}"
user_data = "${data.template_file.user_data.rendered}"
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "scaling_group" {
launch_configuration = "${aws_launch_configuration.instances.id}"
availability_zones = ["${var.availability_zones_names}"]
load_balancers = ["${var.elb_id}"]
health_check_type = "ELB"
min_size = "${var.min_size}"
max_size = "${var.max_size}"
tags = {
key = "Name"
value = "terraformUpAndRunning-${var.cluster_name}"
propagate_at_launch = true
}
wait_for_capacity_timeout = "5m"
}
I have checked the auto scaling group attributes in official Terraform documentation but can't think in any of those to get my aim... Is there any way?

Instances that are managed by an auto-scaling group are not managed by Terraform. Therefore it's unwise for Terraform to keep track of those ephemeral instances in it's state files. Ephemeral instances will come and go by their nature.
However, if you really want to use Terraform for this purpose there is a data source called aws_instances. This gives you the ability to query and list multiple instances.
You could also use awscli to print out the instance details. Here's an example of that: Getting a list of instances in an EC2 auto scale group?.

Related

aws_lambda_alias create a new one and do not update the existant one

is there any way with terraform to create a new lambda alias if the name changeS and do not update the existing one when performing terraform apply
resource "aws_lambda_alias" "test_lambda_alias" {
name = "my_alias"
description = "a sample description"
function_name = aws_lambda_function.lambda_function_test.arn
function_version = "1"
}

Terraform - Adding Validation for type = map(object()) in variables.tf

First thanks for this post Adding a default field for type = map(object()) in variavles.tf, this answered the first part of the struggle that I had in getting default values to work with type map(object()). The last part that I am trying to get working is how to do validation of the input values.
terraform {
experiments = [module_variable_optional_attrs]
}
variable "dns_server" {
description = "Add DNS Servers for domain resolution. You can configure a maximum of two servers. Only one can be preferred 'true'."
type = map(object({
preferred = optional(bool)
server = optional(string)
}))
default = {
default = {
preferred = false
server = "198.18.1.1"
}
}
validation {
condition = (
can(regexall("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$", var.dns_server["server"]))
)
error_message = "The DNS Server is not a valid IPv4 Address."
}
}
locals {
dns_server = {
for k, v in var.dns_server : k => {
preferred = coalesce(v.preferred, false)
server = coalesce(v.server, "198.18.1.1")
}
}
}
The defaults value in the variable field I know isn't used but I am using it as a placeholder for the terraform docs output.
I also know what I have above for validation is not correct because if the user used the default server IPv4 that wouldn't be set until the locals definition. I just don't know of a way to do the validation because I my trusty google search hasn't pulled up any similar examples.
The code is located here if you need more details about how it is being used:
https://github.com/scotttyso/terraform-aci-fabric/tree/main/test
If I comment out the validation everything else is working fine. Thanks in advance.
Is this what you are after?
variable "mapobject" {
type = map(object({
cidr_block = string
destination_type = string
}
))
validation {
condition = alltrue([
for o in var.mapobject : contains(["CIDR_BLOCK","NETWORK_SECURITY_GROUP","SERVICE_CIDR_BLOCK"],o.destination_type)]) error_message = "All destination_types must be one of CIDR_BLOCK,NETWORK_SECURITY_GROUP or SERVICE_CIDR_BLOCK!"
}
}
With a variable assignment of
mapobject = {
"r0" = {cidr_block = "10.1.1.0/24",destination_type = "CIDR_BLOCK" }
}
The validation succeeds, where as the following fails (as required)
mapobject = {
"r0" = {cidr_block = "10.1.1.0/24",destination_type = "CIRD_BLOCK" }
}
Error: Invalid value for variable
on main.tf line 86:
86: variable "mapobject" {
All destination_types must be one of CIDR_BLOCK,NETWORK_SECURITY_GROUP or
SERVICE_CIDR_BLOCK!
This was checked by the validation rule at main.tf:93,2-12.
If it is, then kudos goes here: https://discuss.hashicorp.com/t/validate-list-object-variables/18291/2

Get specific CRM organization instead of all

There are multiple organizations on a CRM on-prem environment. I have stored DiscoveryService URL and organization name in a configuration file.
I want to get an Organization instance for a specific organization using organization name available in the configuration file, instead of loading all organizations.
something similar to,
select organizations from Organizations where orgName = 'XYZ'
Currently, I am using the below code, which is taking more than 3 seconds to retrieve an instance.
private OrganizationDetail DiscoverOrganization(Uri discoveryUri, string organizationName, ClientCredentials lclClientCredentials)
{
DiscoveryServiceProxy serviceProxy;
using (serviceProxy = new DiscoveryServiceProxy(discoveryUri, null, lclClientCredentials, null))
{
IDiscoveryService service = serviceProxy;
var orgsRequest = new RetrieveOrganizationsRequest() { AccessType = EndpointAccessType.Default, Release = OrganizationRelease.Current };
var organizations = (RetrieveOrganizationsResponse)service.Execute(orgsRequest);
return organizations.Details.FirstOrDefault(x => x.UniqueName.ToLower() == organizationName.ToLower());
}
}
You can try web api version as well. Read more
GET https://dev.{servername}/api/discovery/v9.0/Instances(UniqueName='myorg')

Accessing user attributes using UserAccessor

The class com.sap.cloud.sdk.cloudplatform.security.user.UserAccessor allows to me to retrieve the current user and it's attributes.
For example:
Optional<UserAttribute> optionalfirstName = user.getAttribute("firstname");
UserAttribute ua = optionalfirstName.get();
Once I have retrieved the UserAttribute, it has two properites "Name" and "Value". However there is no method available to get the Value. How can I access the value?
Depending on the SAP CP environment you are using, the UserAttribute is an instance of:
SimpleUserAttribute<String> on Neo
CollectionUserAttribute<String> on Cloud Foundry
You can access the respective values by type-casting to the required instance:
if( ua instanceof SimpleUserAttribute ) {
String value = (String) ((SimpleUserAttribute<?>)ua).getValue();
}
else if ( ua instanceof CollectionUserAttribute ) {
Collection<?> values = ((CollectionUserAttribute<?>)ua).getValues();
}
Note: We plan to simplify this in future releases of the SDK so that StringUserAttribute and StringCollectionUserAttribute instances are returned for more convenient consumption.

Grails validation dependent on related domains

I have several domain classes that are related and I am trying to figure out how to implement a constraint that depends on multiple domains. The jist of the problem is:
Asset has many Capacity pool objects
Asset has many Resource objects
When I create/edit a resource, need to check that total resources for an Asset doesn't exceed Capacity.
I created a service method that accomplishes this, but shouldn't this be done via a validator in the Resource domain? My service class as listed below:
def checkCapacityAllocation(Asset asset, VirtualResource newItem) {
// Get total Resources allocated from "asset"
def allAllocated = Resource.createCriteria().list() {
like("asset", asset)
}
def allocArray = allAllocated.toArray()
def allocTotal=0.0
for (def i=0; i<allocArray.length; i++) {
allocTotal = allocTotal.plus(allocArray[i].resourceAllocated)
}
// Get total capacities for "asset"
def allCapacities = AssetCapacity.createCriteria().list() {
like("asset", asset)
}
def capacityArray = allCapacities.toArray()
def capacityTotal = 0.0
for (def i=0; i<capacityArray.length; i++) {
capacityTotal += capacityArray[i].actualAvailableCapacity
}
if (allocTotal > capacityTotal) {
return false
}
}
return true
}
The problem I am having is using this method for validation. I am using the JqGrid plugin (with inline editing) and error reporting is problematic. If I could do this type of validation in the domain it would make things a lot easier. Any suggestions?
Thanks so much!
To use the service method as a validator, you'll need to inject the service into your domain, then add a custom validator that calls it. I think it'll look something like this:
class Asset {
def assetService
static hasMany = [resources: Resource]
static constraints = {
resources(validator: { val, obj ->
obj.assetService.checkCapacityAllocation(obj, val)
})
}
}
How about:
def resourceCount = Resource.countByAsset(assetId)
def assetCapacityCount = AssetCapacity.countByAsset(assetId)
if(resourceCount < assetCapacityCount) return true
return false
HTH

Resources