Skip to content

Commit f71a52d

Browse files
committed
Use ${{ matrix.project }} for IaC linter workflow.
1 parent 6fba9e3 commit f71a52d

File tree

4 files changed

+154
-1
lines changed

4 files changed

+154
-1
lines changed

.github/workflows/pr_iac_lint.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
distribution: 'corretto'
3030
java-version: 11
3131
- name: Build Gradle Setup
32-
working-directory: examples/powertools-examples-core/gradle
32+
working-directory: examples/powertools-examples-core/${{ matrix.project }}
3333
run: |
3434
curl -L -o gradle/wrapper/gradle.zip https:$(cat gradle/wrapper/gradle-wrapper.properties | grep distributionUrl | cut -d ':' -f 2)
3535
unzip gradle/wrapper/gradle.zip -d gradle/wrapper/gradle
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2023 Amazon.com, Inc. or its affiliates.
3+
* Licensed under the Apache License, Version 2.0 (the
4+
* "License"); you may not use this file except in compliance
5+
* with the License. You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*
13+
*/
14+
package helloworld
15+
16+
import com.amazonaws.services.lambda.runtime.Context
17+
import com.amazonaws.services.lambda.runtime.RequestHandler
18+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent
19+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent
20+
import com.amazonaws.xray.entities.Subsegment
21+
import org.apache.logging.log4j.LogManager
22+
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger
23+
import software.amazon.cloudwatchlogs.emf.model.DimensionSet
24+
import software.amazon.cloudwatchlogs.emf.model.Unit
25+
import software.amazon.lambda.powertools.logging.Logging
26+
import software.amazon.lambda.powertools.logging.LoggingUtils
27+
import software.amazon.lambda.powertools.metrics.Metrics
28+
import software.amazon.lambda.powertools.metrics.MetricsUtils
29+
import software.amazon.lambda.powertools.tracing.CaptureMode
30+
import software.amazon.lambda.powertools.tracing.Tracing
31+
import software.amazon.lambda.powertools.tracing.TracingUtils
32+
import java.io.BufferedReader
33+
import java.io.IOException
34+
import java.io.InputStreamReader
35+
import java.net.URL
36+
import java.util.stream.Collectors
37+
38+
/**
39+
* Handler for requests to Lambda function.
40+
*/
41+
42+
class App : RequestHandler<APIGatewayProxyRequestEvent?, APIGatewayProxyResponseEvent> {
43+
@Logging(logEvent = true, samplingRate = 0.7)
44+
@Tracing(captureMode = CaptureMode.RESPONSE_AND_ERROR)
45+
@Metrics(namespace = "ServerlessAirline", service = "payment", captureColdStart = true)
46+
47+
override fun handleRequest(input: APIGatewayProxyRequestEvent?, context: Context?): APIGatewayProxyResponseEvent {
48+
val headers = mapOf("Content-Type" to "application/json", "X-Custom-Header" to "application/json")
49+
MetricsUtils.metricsLogger().putMetric("CustomMetric1", 1.0, Unit.COUNT)
50+
MetricsUtils.withSingleMetric("CustomMetrics2", 1.0, Unit.COUNT, "Another") { metric: MetricsLogger ->
51+
metric.setDimensions(DimensionSet.of("AnotherService", "CustomService"))
52+
metric.setDimensions(DimensionSet.of("AnotherService1", "CustomService1"))
53+
}
54+
LoggingUtils.appendKey("test", "willBeLogged")
55+
val response = APIGatewayProxyResponseEvent().withHeaders(headers)
56+
return try {
57+
val pageContents = getPageContents("https://checkip.amazonaws.com")
58+
log.info(pageContents)
59+
TracingUtils.putAnnotation("Test", "New")
60+
val output = """
61+
{
62+
"message": "hello world",
63+
"location": "$pageContents"
64+
}
65+
""".trimIndent()
66+
TracingUtils.withSubsegment("loggingResponse") { _: Subsegment? ->
67+
val sampled = "log something out"
68+
log.info(sampled)
69+
log.info(output)
70+
}
71+
log.info("After output")
72+
response.withStatusCode(200).withBody(output)
73+
} catch (e: RuntimeException) {
74+
response.withBody("{}").withStatusCode(500)
75+
} catch (e: IOException) {
76+
response.withBody("{}").withStatusCode(500)
77+
}
78+
}
79+
80+
@Tracing
81+
private fun log() {
82+
log.info("inside threaded logging for function")
83+
}
84+
85+
@Tracing(namespace = "getPageContents", captureMode = CaptureMode.DISABLED)
86+
@Throws(IOException::class)
87+
private fun getPageContents(address: String): String {
88+
val url = URL(address)
89+
TracingUtils.putMetadata("getPageContents", address)
90+
return InputStreamReader(url.openStream()).use { reader ->
91+
reader.readText().trim()
92+
}
93+
}
94+
95+
private val log = LogManager.getLogger(App::class)
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2023 Amazon.com, Inc. or its affiliates.
3+
* Licensed under the Apache License, Version 2.0 (the
4+
* "License"); you may not use this file except in compliance
5+
* with the License. You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*
13+
*/
14+
package helloworld
15+
16+
import com.amazonaws.services.lambda.runtime.Context
17+
import com.amazonaws.services.lambda.runtime.RequestStreamHandler
18+
import com.fasterxml.jackson.databind.ObjectMapper
19+
import software.amazon.lambda.powertools.logging.Logging
20+
import software.amazon.lambda.powertools.metrics.Metrics
21+
import java.io.IOException
22+
import java.io.InputStream
23+
import java.io.OutputStream
24+
25+
class AppStream : RequestStreamHandler {
26+
@Logging(logEvent = true)
27+
@Metrics(namespace = "ServerlessAirline", service = "payment", captureColdStart = true)
28+
@Throws(IOException::class)
29+
override fun handleRequest(input: InputStream, output: OutputStream, context: Context) {
30+
val map: Map<*, *> = mapper.readValue(input, MutableMap::class.java)
31+
println(map.size)
32+
}
33+
34+
companion object {
35+
private val mapper = ObjectMapper()
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package helloworld
2+
3+
4+
import org.junit.Assert
5+
import org.junit.Test
6+
7+
class AppTest {
8+
@Test
9+
fun successfulResponse() {
10+
val app = App()
11+
val result = app.handleRequest(null, null)
12+
Assert.assertEquals(200, result.statusCode.toLong())
13+
Assert.assertEquals("application/json", result.headers["Content-Type"])
14+
val content = result.body
15+
Assert.assertNotNull(content)
16+
Assert.assertTrue(""""message"""" in content)
17+
Assert.assertTrue(""""hello world"""" in content)
18+
Assert.assertTrue(""""location"""" in content)
19+
}
20+
}

0 commit comments

Comments
 (0)