properties = new LinkedHashMap<>();
+
+ public EcsTaskMetadataPropertySource(String context, EcsTaskMetadataResolver ecsTaskMetadataResolver) {
+ super(context, ecsTaskMetadataResolver);
+ this.context = context;
+ this.ecsTaskMetadataResolver = ecsTaskMetadataResolver;
+ }
+
+ @Override
+ public EcsTaskMetadataPropertySource copy() {
+ return new EcsTaskMetadataPropertySource(context, source);
+ }
+
+ @Override
+ public void init() {
+ properties.putAll(ecsTaskMetadataResolver.getEcsTaskMetadata());
+ }
+
+ @Override
+ public String[] getPropertyNames() {
+ return properties.keySet().toArray(String[]::new);
+ }
+
+ @Override
+ @Nullable
+ public Object getProperty(String name) {
+ return properties.get(name);
+ }
+}
diff --git a/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/ecs/EcsTaskMetadataResolver.java b/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/ecs/EcsTaskMetadataResolver.java
new file mode 100644
index 000000000..ae1e74b10
--- /dev/null
+++ b/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/ecs/EcsTaskMetadataResolver.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2013-2022 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package io.awspring.cloud.autoconfigure.ecs;
+
+import java.util.*;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.MediaType;
+import org.springframework.web.client.RestClient;
+import org.springframework.web.client.RestClient.Builder;
+import org.springframework.web.client.RestClientException;
+
+/**
+ * Resolves ECS task metadata. Supports ECS task metadata endpoint version 4.
+ *
+ * See AWS
+ * documentation for more details.
+ *
+ * @author Jukka Palomäki
+ * @since 3.5.0
+ */
+public class EcsTaskMetadataResolver {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(EcsTaskMetadataResolver.class);
+ private static final String ECS_CONTAINER_METADATA_URI_V4 = System.getenv("ECS_CONTAINER_METADATA_URI_V4");
+
+ /** We expose a subset of available task metadata */
+ private static final Set EXPOSED_PROPERTIES = Set.of("Cluster", "ServiceName", "TaskARN", "Family",
+ "Revision", "AvailabilityZone", "LaunchType");
+
+ private final RestClient restClient;
+
+ public EcsTaskMetadataResolver(Builder restClientBuilder) {
+ this.restClient = restClientBuilder.baseUrl(ECS_CONTAINER_METADATA_URI_V4).build();
+ }
+
+ /**
+ * Returns ECS task metadata. If unable to fetch the metadata, an empty Map is returned.
+ */
+ public Map getEcsTaskMetadata() {
+ Map properties = new LinkedHashMap<>();
+ tryPopulateTaskMetadata(properties);
+ return properties;
+ }
+
+ @SuppressWarnings("unchecked")
+ private void tryPopulateTaskMetadata(Map properties) {
+ try {
+ LOGGER.debug("Getting ECS task metadata from {}/task", ECS_CONTAINER_METADATA_URI_V4);
+ Map taskMetadata = restClient.get().uri("/task").accept(MediaType.APPLICATION_JSON)
+ .retrieve().body(Map.class);
+ if (taskMetadata != null) {
+ for (String key : EXPOSED_PROPERTIES) {
+ if (taskMetadata.containsKey(key)) {
+ properties.put("ecs-task-metadata." + key, taskMetadata.get(key).toString());
+ }
+ }
+ }
+ } catch (IllegalArgumentException | RestClientException e) {
+ LOGGER.error("Error getting ECS task metadata", e);
+ }
+ }
+}
diff --git a/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/ecs/package-info.java b/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/ecs/package-info.java
new file mode 100644
index 000000000..68b653c8b
--- /dev/null
+++ b/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/ecs/package-info.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright 2013-2022 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Auto-configuration for ECS (Elastic Container Service) integration.
+ */
+@org.springframework.lang.NonNullApi
+@org.springframework.lang.NonNullFields
+package io.awspring.cloud.autoconfigure.ecs;
diff --git a/spring-cloud-aws-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/spring-cloud-aws-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
index 657cbf245..3c1fe936f 100644
--- a/spring-cloud-aws-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
+++ b/spring-cloud-aws-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
@@ -15,3 +15,4 @@ io.awspring.cloud.autoconfigure.config.secretsmanager.SecretsManagerAutoConfigur
io.awspring.cloud.autoconfigure.config.parameterstore.ParameterStoreReloadAutoConfiguration
io.awspring.cloud.autoconfigure.config.parameterstore.ParameterStoreAutoConfiguration
io.awspring.cloud.autoconfigure.config.s3.S3ReloadAutoConfiguration
+io.awspring.cloud.autoconfigure.ecs.EcsTaskMetadataAutoConfiguration
diff --git a/spring-cloud-aws-starters/spring-cloud-aws-starter-ecs/pom.xml b/spring-cloud-aws-starters/spring-cloud-aws-starter-ecs/pom.xml
new file mode 100644
index 000000000..c73a74e99
--- /dev/null
+++ b/spring-cloud-aws-starters/spring-cloud-aws-starter-ecs/pom.xml
@@ -0,0 +1,23 @@
+
+
+
+ spring-cloud-aws
+ io.awspring.cloud
+ 4.0.0-SNAPSHOT
+ ../../pom.xml
+
+ 4.0.0
+
+ Spring Cloud ECS Starter
+ spring-cloud-aws-starter-ecs
+
+
+
+ io.awspring.cloud
+ spring-cloud-aws-starter
+
+
+
+