Skip to content

Commit

Permalink
Add resource Time::Offset (#188)
Browse files Browse the repository at this point in the history
* Add Time::Offset resource
  • Loading branch information
kddejong authored Apr 5, 2023
1 parent bcf8bad commit 38d1f87
Show file tree
Hide file tree
Showing 32 changed files with 1,206 additions and 26 deletions.
95 changes: 95 additions & 0 deletions release/awscommunity/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,19 @@ Resources:
ManagedPolicyArns:
- Fn::ImportValue: !Sub "cep-${Env}-common-build-project-policy"

TimeOffsetBuildProjectRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service: codebuild.amazonaws.com
Version: '2012-10-17'
ManagedPolicyArns:
- Fn::ImportValue: !Sub "cep-${Env}-common-build-project-policy"

CloudFrontWebAclAssociationBuildProjectRole:
Type: AWS::IAM::Role
Properties:
Expand Down Expand Up @@ -456,6 +469,40 @@ Resources:
Roles:
- !Ref TimeSleepBuildProjectRole

TimeOffsetBuildProjectRolePolicy:
Type: AWS::IAM::Policy
Properties:
PolicyDocument:
Statement:
- Action:
- iam:CreateRole
- iam:DeleteRole
- iam:GetRole
- dynamodb:*
Effect: Allow
Resource: "*"
- Action:
- codebuild:StartBuild
- codebuild:BatchGetBuilds
- codebuild:StopBuild
- codebuild:RetryBuild
- codebuild:StartBuildBatch
- codebuild:RetryBuildBatch
- codebuild:StopBuildBatch
Effect: Allow
Resource:
- !GetAtt TimeOffsetBuildProject.Arn
- Action:
- ssm:DeleteParameter
- ssm:GetParameter
- ssm:PutParameter
Effect: Allow
Resource: "*"
Version: '2012-10-17'
PolicyName: delete-bucket-contents-build-project-policy
Roles:
- !Ref TimeOffsetBuildProjectRole

CloudFrontWebAclAssociationBuildProjectPolicy:
Type: AWS::IAM::Policy
Properties:
Expand Down Expand Up @@ -719,6 +766,28 @@ Resources:
BuildSpec: !Sub "resources/${Env}-buildspec-go.yml"
TimeoutInMinutes: 480

TimeOffsetBuildProject:
Type: AWS::CodeBuild::Project
Properties:
Name: !Sub "${PrefixLower}-${Env}-time-offset"
Artifacts:
Type: CODEPIPELINE
Environment:
ComputeType: BUILD_GENERAL1_LARGE
Image: !Sub "${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/cep-cicd:latest"
ImagePullCredentialsType: SERVICE_ROLE
PrivilegedMode: true
Type: LINUX_CONTAINER
EnvironmentVariables:
- Name: RESOURCE_PATH
Type: PLAINTEXT
Value: "placeholder-for-path-to-resource"
ServiceRole: !GetAtt TimeOffsetBuildProjectRole.Arn
Source:
Type: CODEPIPELINE
BuildSpec: !Sub "resources/${Env}-buildspec-go.yml"
TimeoutInMinutes: 480

S3BucketVersioningEnabledBuildProject:
Type: AWS::CodeBuild::Project
Properties:
Expand Down Expand Up @@ -992,6 +1061,7 @@ Resources:
- !GetAtt S3PublicAccessControlsRestrictedBuildProject.Arn
- !GetAtt TimeStaticBuildProject.Arn
- !GetAtt TimeSleepBuildProject.Arn
- !GetAtt TimeOffsetBuildProject.Arn
- !GetAtt CloudFrontLoggingEnabledBuildProject.Arn
- !GetAtt CloudFrontWebAclAssociationBuildProject.Arn
- !GetAtt S3BucketModuleBuildProject.Arn
Expand Down Expand Up @@ -1058,6 +1128,7 @@ Resources:
- !GetAtt S3PublicAccessControlsRestrictedBuildProjectRole.Arn
- !GetAtt TimeStaticBuildProjectRole.Arn
- !GetAtt TimeSleepBuildProjectRole.Arn
- !GetAtt TimeOffsetBuildProjectRole.Arn
- !GetAtt AlternateContactBuildProjectRole.Arn
- !GetAtt CloudFrontLoggingEnabledBuildProjectRole.Arn
- !GetAtt CloudFrontWebAclAssociationBuildProjectRole.Arn
Expand Down Expand Up @@ -1240,6 +1311,30 @@ Resources:
}
]
RunOrder: 1
- Name: TimeOffset
InputArtifacts:
- Name: extensions-source
ActionTypeId:
Category: Build
Owner: AWS
Provider: CodeBuild
Version: 1
Configuration:
ProjectName: !Ref TimeOffsetBuildProject
# BatchEnabled:
# !If
# - IsProd
# - true
# - false
EnvironmentVariables: |-
[
{
"name": "RESOURCE_PATH",
"type": "PLAINTEXT",
"value": "resources/Time_Offset"
}
]
RunOrder: 1
- Name: Account_AlternateContact
InputArtifacts:
- Name: extensions-source
Expand Down
17 changes: 17 additions & 0 deletions resources/Time_Offset/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# macOS
.DS_Store
._*

# our logs
rpdk.log*

#compiled file
bin/

#vender
vender/

# contains credentials
sam-tests/
.hypothesis/
/awscommunity-time-offset.zip
21 changes: 21 additions & 0 deletions resources/Time_Offset/.rpdk-config
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"artifact_type": "RESOURCE",
"typeName": "AwsCommunity::Time::Offset",
"language": "go",
"runtime": "go1.x",
"entrypoint": "handler",
"testEntrypoint": "handler",
"settings": {
"version": false,
"subparser_name": null,
"verbose": 0,
"force": false,
"type_name": null,
"artifact_type": null,
"endpoint_url": null,
"region": null,
"target_schemas": [],
"import_path": "github.com/aws-cloudformation/awscommunity-registry-extensions/resources/Time_Offset",
"protocolVersion": "2.0.0"
}
}
11 changes: 11 additions & 0 deletions resources/Time_Offset/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.PHONY: build test clean

build:
make -f makebuild # this runs build steps required by the cfn cli

test:
cfn generate
env GOOS=linux go build -ldflags="-s -w" -o bin/handler cmd/main.go

clean:
rm -rf bin
74 changes: 74 additions & 0 deletions resources/Time_Offset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# AwsCommunity::Time::Offset

Creates a time based resource with an offset.

## Example

```yaml
Resources:
StarTime:
Type: AwsCommunity::Time::Offset
Properties:
OffsetHours: 1
EndTime:
Type: AwsCommunity::Time::Offset
Properties:
OffsetYears: 5
DailyNotice:
Type: AWS::Pinpoint::Campaign
Properties:
...
Schedule:
EndTime: !GetAtt EndTime.Utc
Frequency: "DAILY"
IsLocalTime: true
StartTime: !GetAtt StartTime.Utc
TimeZone: "UTC-07"
```
## Properties
| Properties | Type | Description |
| ------------- | ------------- | ------------- |
| **Time** | string | Utc time to do the offset from. If none is provided now will be used.
| **OffsetYears** | integer | Number of years to offset the base timestamp.
| **OffsetMonths** | integer | Number of months to offset the base timestamp.
| **OffsetDays** | integer | Number of days to offset the base timestamp.
| **OffsetHours** | integer | Number of hours to offset the base timestamp.
| **OffsetMinutes** | integer | Number of minutes to offset the base timestamp.
| **OffsetSeconds** | integer | Number of seconds to offset the base timestamp.
## Attributes
The following properties you can use in a `GetAtt`

| Attribute | Type | Description |
| ------------- | ------------- | ------------- |
| **Id** | string | Unique ID that identifies the resource.
| **Day** | integer | Day returns the day of the month.
| **Hour** | integer | Hour returns the hour within the day, in the range [0, 23].
| **Minute** | integer | Minute returns the minute offset within the hour, in the range [0, 59].
| **Month** | integer | Month returns the month of the year.
| **Second** | integer | Second returns the second offset within the minute, in the range [0, 59].
| **Unix** | integer | Unix returns a Unix time, the number of seconds elapsed since January 1, 1970 UTC.
| **Utc** | string | The time represented in UTC time
| **Year** | integer | Year returns the year.

## Development

Open two tabs in your terminal.

Run SAM
```sh
sam local start-lambda
```

In another tab, run cfn test:

```sh
cd resources/Time_Offset
cfn test
```

## Notes

### SSM Parameter
Since this extension does not represent an actual resource, we have to create a Systems Manager Parameter Store entry to track its state for the purposes of satisfying the CloudFormation registry contract. We store a key in parameter store at `/CloudFormation/AwsCommunity/Time/Offset/unique-identifier`.
Loading

0 comments on commit 38d1f87

Please sign in to comment.