Skip to content

Automatically install Amazon.Lambda.TestTool #28

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

Merged
merged 4 commits into from
Feb 7, 2025
Merged

Conversation

normj
Copy link
Member

@normj normj commented Feb 6, 2025

Description of changes:
If the Lambda service emulator is added there will be a check made to see if Amazon.Lambda.TestTool is installed and up to date. If not then it will be installed via the dotnet tool install command.

The Lambda service emulator is added to the collection resources as part of the first invoke of AddAWSLambdaFunction. If the user wants to customize the installation or disable it they can call the new AddAWSLambdaServiceEmulator method before calling AddAWSLambdaFunction. The AddAWSLambdaServiceEmulator takes in an LambdaEmulatorOptions that allows disabling the auto install, overriding the version to install or allowing downgrading the version. By default if a newer version of the tooling is already installed then what the Aspire.Hosting.AWS is looking for we will allow assuming the user knows what they are doing.

While going through the area I did some minor clean work.

  • Fix all the namespaces of the classes in the unit tests projects
  • Made the pattern of adding the Aspire ExecutableResource that represents the emulators the same across Lambda and API Gateway. This including some class renames.

This PR is depending on the Amazon.Lambda.TestTool [PR#](aws/aws-lambda-dotnet#1969) that adds a new --tool-info switch that Aspire can used to get metadata about the currently installed version.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@normj normj force-pushed the normj/install-testtool branch from c0ad606 to fee0285 Compare February 6, 2025 08:21
using System.Diagnostics;
using System.Text;

namespace Aspire.Hosting.AWS.Utils.Internal;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to expose this interface as part of our public API but for unit testing purposes I need to implement the interface and making the interface internal doesn't work for that even marking the assembly internal visible to. So I went with the compromise of putting this in an "Internal" namespace.

@normj normj force-pushed the normj/install-testtool branch from fee0285 to ffbb42b Compare February 6, 2025 08:27
namespace Aspire.Hosting.AWS.Lambda;

/// <summary>
/// Lambda lifecycle hook takes care of getting Amazon.Lambda.TestTool is installed if there was
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: is installed -> installed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this change, but why does the entire solution has 1 Directory.Packages.props? So anytime we add a new package, we are adding it for the entire solution?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The repo was setup so that it had centralized package versioning. But in the individual project files you still specify the package you want to use. But in the project you don't set a version. For example in the project file of Aspire.Hosting.AWS we add the packages but don't set the version.

  <ItemGroup>
    <PackageReference Include="Aspire.Hosting" />
    <PackageReference Include="Amazon.CDK.Lib" />
    <PackageReference Include="AWSSDK.Core" />
    <PackageReference Include="AWSSDK.CloudFormation" />
  </ItemGroup>

This way you only have to update the version of a package in one place for all projects in a repository.

{
internal void AddCommandLineArguments(IList<object> arguments)
{
arguments.Add("lambda-test-tool");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't the name of our tool dotnet-lambda-test-tool?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apparently dotnet lambda-test-tool works and so does dotnet-lambda-test-tool.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed in sync meeting and we are on the same page now.

@@ -14,4 +14,22 @@ internal class LambdaEmulatorAnnotation(EndpointReference endpoint) : IResourceA
/// The HTTP endpoint for the Lambda runtime emulator.
/// </summary>
public EndpointReference Endpoint { get; init; } = endpoint;

/// <summary>
/// If set to true Amazon.Lambda.TestTool will updated/installed during AppHost startup. Amazon.Lambda.TestTool is
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the name and the description are saying 2 different things. The name suggests that we won't auto install, but the description is saying that we will?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

namespace Aspire.Hosting.AWS.Lambda;

/// <summary>
/// Options that can be added the Lambda emulator resource.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add "to" between "added" and "the"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

public class LambdaEmulatorOptions
{
/// <summary>
/// If set to true, Amazon.Lambda.TestTool will updated/installed during AppHost startup. Amazon.Lambda.TestTool is
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as before

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

{
internal void AddCommandLineArguments(IList<object> arguments)
{
arguments.Add("lambda-test-tool");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question on tool name

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also didn't we change the behavior so that you need to specify the port for the emulator to run? won't this error out as is?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed in sync meeting.

{
if (builder.Resources.FirstOrDefault(x => x.TryGetAnnotationsOfType<LambdaEmulatorAnnotation>(out _)) is not ExecutableResource serviceEmulator)
/// <summary>
/// Add the Lambda service emulator resource. The AddAWSLambdaFunction method will automatically add the Lambda service emulator if it hasn't
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add doc references to "AddAWSLambdaFunction" and "LambdaEmulatorOptions"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


private async Task<string> GetCurrentInstalledVersionAsync(CancellationToken cancellationToken)
{
var results = await processCommandService.RunProcessAndCaptureOuputAsync(logger, "dotnet", "lambda-test-tool --tool-info", cancellationToken);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might need to update based on other PR changes aws/aws-lambda-dotnet#1969

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@normj normj force-pushed the normj/install-testtool branch from dbba606 to 0de32d3 Compare February 7, 2025 00:07
@normj normj merged commit 9769bd1 into dev Feb 7, 2025
5 checks passed
@normj normj deleted the normj/install-testtool branch February 7, 2025 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants