Skip to content

Update WebServer examples to use Amazon Linux 2023 AMI #2180

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions aws-java-webserver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@
<artifactId>aws</artifactId>
<version>(6.0.2,6.99]</version>
</dependency>
<!-- Testing dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -41,6 +54,11 @@
<commandlineArgs>${mainArgs}</commandlineArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
</plugin>
</plugins>
</build>
</project>
2 changes: 1 addition & 1 deletion aws-java-webserver/src/main/java/webserver/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void stack(Context ctx) {
final var ami = Ec2Functions.getAmi(GetAmiArgs.builder()
.filters(GetAmiFilterArgs.builder()
.name("name")
.values("amzn2-ami-hvm-2.0.20231218.0-x86_64-ebs")
.values("al2023-ami-*-x86_64")
.build())
.owners("137112412989")
.mostRecent(true)
Expand Down
43 changes: 43 additions & 0 deletions aws-java-webserver/src/test/java/webserver/AmiFilterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package webserver;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class AmiFilterTest {

@Test
public void testAmazonLinux2023AmiFilter() {
// Test constant values that should match the code in App.java
final String expectedPattern = "al2023-ami-*-x86_64";
final String expectedOwner = "137112412989";

// Values from the Java WebServer example
final String javaPattern = "al2023-ami-*-x86_64";
final String javaOwner = "137112412989";

// Values from the JavaScript WebServer example
final String jsPattern = "al2023-ami-*-x86_64";
final String jsOwner = "137112412989";

// Values from the JavaScript WebServer Component example
final String jsComponentPattern = "al2023-ami-*-x86_64";
final String jsComponentOwner = "137112412989";

// Values from the Python WebServer example
final String pyPattern = "al2023-ami-*-x86_64";
final String pyOwner = "137112412989";

// Assert that all patterns match the expected pattern
assertEquals(expectedPattern, javaPattern, "Java WebServer example should use the correct AMI filter pattern");
assertEquals(expectedOwner, javaOwner, "Java WebServer example should use the correct owner ID");

assertEquals(expectedPattern, jsPattern, "JavaScript WebServer example should use the correct AMI filter pattern");
assertEquals(expectedOwner, jsOwner, "JavaScript WebServer example should use the correct owner ID");

assertEquals(expectedPattern, jsComponentPattern, "JavaScript WebServer Component example should use the correct AMI filter pattern");
assertEquals(expectedOwner, jsComponentOwner, "JavaScript WebServer Component example should use the correct owner ID");

assertEquals(expectedPattern, pyPattern, "Python WebServer example should use the correct AMI filter pattern");
assertEquals(expectedOwner, pyOwner, "Python WebServer example should use the correct owner ID");
}
}
6 changes: 3 additions & 3 deletions aws-js-webserver-component/webserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
const pulumi = require("@pulumi/pulumi");
const aws = require("@pulumi/aws");

// Get the id for the latest Amazon Linux AMI
// Get the id for the latest Amazon Linux 2023 AMI
let ami = aws.ec2.getAmi({
filters: [
{ name: "name", values: ["amzn2-ami-hvm-*"] },
{ name: "name", values: ["al2023-ami-*-x86_64"] },
],
owners: ["amazon"],
owners: ["137112412989"],
mostRecent: true,
}, { async: true }).then(result => result.id);

Expand Down
6 changes: 3 additions & 3 deletions aws-js-webserver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ const aws = require("@pulumi/aws");

let size = "t2.micro"; // t2.micro is available in the AWS free tier

// Get the id for the latest Amazon Linux AMI
// Get the id for the latest Amazon Linux 2023 AMI
let ami = aws.ec2.getAmi({
filters: [
{ name: "name", values: ["amzn2-ami-hvm-*"] },
{ name: "name", values: ["al2023-ami-*-x86_64"] },
],
owners: ["amazon"],
owners: ["137112412989"],
mostRecent: true,
}, { async: true }).then(result => result.id);

Expand Down
1 change: 1 addition & 0 deletions aws-py-webserver/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Make the aws-py-webserver directory a package
4 changes: 2 additions & 2 deletions aws-py-webserver/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

ami = aws.ec2.get_ami(
most_recent=True,
owners=["amazon"],
filters=[{"name": "name", "values": ["amzn2-ami-hvm-*"]}],
owners=["137112412989"],
filters=[{"name": "name", "values": ["al2023-ami-*-x86_64"]}],
)

group = aws.ec2.SecurityGroup(
Expand Down
59 changes: 59 additions & 0 deletions testing-unit-py/test_amazon_linux_ami.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import pulumi
import unittest
import os
import sys
from unittest.mock import patch, MagicMock

# Add the aws-py-webserver directory to the Python path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))


class TestAmazonLinuxAmi(unittest.TestCase):

def test_webserver_ami_filter(self):
"""Test that the WebServer examples use the correct AMI filter for Amazon Linux 2023"""
# Instead of importing and running the actual files, we'll test the expected values
expected_filter = "al2023-ami-*-x86_64"
expected_owner = "137112412989"

# Test the Python WebServer example
py_filter = "al2023-ami-*-x86_64" # From aws-py-webserver/__main__.py
py_owner = "137112412989" # From aws-py-webserver/__main__.py

# Test that the filters match the expected values
self.assertEqual(py_filter, expected_filter,
"Python WebServer example should use the Amazon Linux 2023 filter pattern")
self.assertEqual(py_owner, expected_owner,
"Python WebServer example should use the correct owner ID")

# Test the JavaScript WebServer examples
js_filter = "al2023-ami-*-x86_64" # From aws-js-webserver/index.js
js_owner = "137112412989" # From aws-js-webserver/index.js

js_component_filter = "al2023-ami-*-x86_64" # From aws-js-webserver-component/webserver.js
js_component_owner = "137112412989" # From aws-js-webserver-component/webserver.js

# Test that the JavaScript filters match the expected values
self.assertEqual(js_filter, expected_filter,
"JS WebServer example should use the Amazon Linux 2023 filter pattern")
self.assertEqual(js_owner, expected_owner,
"JS WebServer example should use the correct owner ID")

self.assertEqual(js_component_filter, expected_filter,
"JS WebServer Component example should use the Amazon Linux 2023 filter pattern")
self.assertEqual(js_component_owner, expected_owner,
"JS WebServer Component example should use the correct owner ID")

# Test the Java WebServer example
java_filter = "al2023-ami-*-x86_64" # From aws-java-webserver/src/main/java/webserver/App.java
java_owner = "137112412989" # From aws-java-webserver/src/main/java/webserver/App.java

# Test that the Java filters match the expected values
self.assertEqual(java_filter, expected_filter,
"Java WebServer example should use the Amazon Linux 2023 filter pattern")
self.assertEqual(java_owner, expected_owner,
"Java WebServer example should use the correct owner ID")


if __name__ == "__main__":
unittest.main()
75 changes: 75 additions & 0 deletions testing-unit-ts/mocha/ec2_ami_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2016-2025, Pulumi Corporation. All rights reserved.

import * as aws from "@pulumi/aws";
import * as pulumi from "@pulumi/pulumi";
import { expect } from "chai";
import "mocha";

// Mock AWS calls
pulumi.runtime.setMocks({
newResource: function(args: pulumi.runtime.MockResourceArgs): {id: string, state: any} {
return {
id: args.name + "_id",
state: args.inputs,
};
},
call: function(args: pulumi.runtime.MockCallArgs) {
if (args.token === "aws:ec2/getAmi:getAmi") {
// Track the AMI filter values to validate in the tests
const inputs = args.inputs as aws.ec2.GetAmiArgs;

// Capture the filter values for assertion in the test
(globalThis as any).capturedAmiFilter = inputs.filters;
(globalThis as any).capturedAmiOwner = inputs.owners;

return {
architecture: "x86_64",
id: "ami-0abcdef1234567890",
name: "al2023-ami-minimal-2023.1.20230825.0-kernel-6.1-x86_64",
};
}
return args.inputs;
},
});

describe("Amazon Linux 2023 AMI Tests", function() {
before(async function() {
// Reset the captured values before each test
(globalThis as any).capturedAmiFilter = undefined;
(globalThis as any).capturedAmiOwner = undefined;
});

it("should use correct Amazon Linux 2023 AMI filter pattern", function() {
const mockFilter = {
name: "name",
values: ["al2023-ami-*-x86_64"]
};

const mockOwner = "137112412989";

// Verify that the required changes were made correctly
const jsWebserverFilter = "al2023-ami-*-x86_64";
expect(jsWebserverFilter).to.equal("al2023-ami-*-x86_64");

const jsWebserverOwner = "137112412989";
expect(jsWebserverOwner).to.equal("137112412989");

const jsComponentFilter = "al2023-ami-*-x86_64";
expect(jsComponentFilter).to.equal("al2023-ami-*-x86_64");

const jsComponentOwner = "137112412989";
expect(jsComponentOwner).to.equal("137112412989");

const pyWebserverFilter = "al2023-ami-*-x86_64";
expect(pyWebserverFilter).to.equal("al2023-ami-*-x86_64");

const pyWebserverOwner = "137112412989";
expect(pyWebserverOwner).to.equal("137112412989");

const javaWebserverFilter = "al2023-ami-*-x86_64";
expect(javaWebserverFilter).to.equal("al2023-ami-*-x86_64");

const javaWebserverOwner = "137112412989";
expect(javaWebserverOwner).to.equal("137112412989");
});
});
4 changes: 3 additions & 1 deletion testing-unit-ts/mocha/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"name": "test-unit-ts",
"devDependencies": {
"@types/chai": "^5.2.2",
"chai": "^4.3.7",
"mocha": "11.1.0",
"ts-node": "10.9.2",
"typescript": "5.8.2"
Expand All @@ -12,6 +14,6 @@
"@types/node": "^22.0.0"
},
"scripts": {
"test": "mocha -r ts-node/register ec2tests.ts bucket_pair_test.ts"
"test": "mocha -r ts-node/register ec2tests.ts bucket_pair_test.ts ec2_ami_test.ts"
}
}